1

After using many python scripts with the same Tensorflow FLAGS, I got tired of updating multiple flags for each change and so decided to refactor tf.app.flags into a separate class which I could reuse across scripts.

However, for some strange reason, whenever I use self.flags in a different method it fails to recognise a previously set flag. The following class for example will work fine for flag project_dir2 but will fail for flag project_dir3

`class MyClass():
   def __init__(self):
     self.flags = tf.app.flags
     self.FLAGS = self.flags.FLAGS

     #test code that works here
     self.flags.DEFINE_string("project_dir2", "aValue", "project directory")
     print("This will print correctly: "+self.FLAGS.project_dir2)
     self.my_function()

  def my_function(self):
     #test code that fails
     self.flags.DEFINE_string("project_dir3", "aValue", "project directory")
     print("This will fail: "+self.FLAGS.project_dir3)`

I get the following exception:

AttributeError: project_dir2 Exception TypeError: TypeError("'NoneType' object is not callable",) in <function _remove at 0x7fd4c3090668> ignored

Is there something obvious I'm doing wrong? Or is this something you simply cannot do with Tensorflow flags? Does that mean there's no way of refactoring commonly used flag settings across scripts?

1 Answers1

0

It seems there's an internal method called _parse_flags() which is called on first access. You could call it manually after you update it

IE

  def my_function(self):
    #test code that fails
    self.flags.DEFINE_string("project_dir3", "aValue", "project directory")
    self.flags.FLAGS._parse_flags()

Background on tf.flags - it's a partial re-implementation of Google's gflags library so it's missing features/documentation. It's a contributions to do something smarter like plug in official gflags (issue 1258). That allow things like controlling verbose logging (which requires a recompile right now)

Community
  • 1
  • 1
Yaroslav Bulatov
  • 57,332
  • 22
  • 139
  • 197