Why is FLAGS necessary?

source url: https://stackoverflow.com/questions/43437590/why-is-flags-necessary

Usually FLAGS are used to pass command line arguments into your program. E.g.

import tensorflow as tf
fs = tf.app.flags
fs.DEFINE_integer('n_epochs', 25, 'number of epochs to train [25]')
FLAGS = fs.FLAGS

def main(argv):
    print(FLAGS.n_epochs)

if __name__ == '__main__':
    tf.app.run()

If you run this snippet from the command line as python snippet.py, it will print

25

If you run python snippet.py --n_epochs 50 it will print

50

You could achieve the same thing with python's package argparse.

In the example you posted the use of FLAGS is admittedly a bit strange. Here it could be replaced by just directly defining the variables, unless the FLAGS variable is used someplace else in the code, which you are not showing here.


猜你喜欢

转载自blog.csdn.net/helei001/article/details/79148530
今日推荐