I am toying around with flags at the moment and came across some weird behavior when using tf.app.run()
. The following code snippet should simply print the string given via the command line.
import tensorflow as tf
# command line flags
tf.app.flags.DEFINE_string('mystring', 'Hello World!',
'''String to print to console.''')
FLAGS = tf.app.flags.FLAGS
def main():
print(FLAGS.mystring)
if __name__ == '__main__':
tf.app.run()
During execution, this error is thrown:
Traceback (most recent call last):
File "", line 1, in runfile('/path/flags.py', wdir='/path')
File "/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 710, in runfile execfile(filename, namespace)
File "/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/spyder/utils/site/sitecustomize.py", line 101, in execfile exec(compile(f.read(), filename, 'exec'), namespace)
File "/path/flags.py", line 19, in tf.app.run()
File "/home/abc/anaconda3/envs/tensorflow/lib/python3.5/site-packages/tensorflow/python/platform/app.py", line 126, in run _sys.exit(main(argv))
TypeError: main() takes 0 positional arguments but 1 was given
...which is strange because I do not give a single argument to main(). However, if I add an underscore def main(_):
, it works without any errors.
I couldn't find a doc where this is use of the underscore is described. Does anybody know what happens here? Thank you!