关于 abseil 库标志的说明
Clarification regarding abseil library flags
绳降文档中有如下说法:
When one Python module imports another, it gains access to the other’s flags. (This behavior is implemented by having all modules share a common, global registry object containing all the flag information.)
当一个模块导入另一个模块时——获得对另一个模块标志的访问权限——是否反过来?换句话说,模型 被导入 是否也可以访问导入模型的标志?就行为而言,情况似乎是这样。一般规则是否应该是任何两个具有某种导入连接的 python 文件都可以访问彼此的标志,并且这种情况是可传递的?
以下三个文件展示了我所指的行为。
sample.py
from absl import app
from absl import flags
import sample2
FLAGS = flags.FLAGS
flags.DEFINE_string('name', 'Jane Random', 'Your name.')
def main(argv):
print(FLAGS.name, "is being called in sample.py.")
print(FLAGS.Flag2) #Flag2 is a flag of sample3.py.
sample2.Sample()
if __name__ == '__main__':
app.run(main)
sample2.py:
from absl import app
from absl import flags
import sample3
FLAGS = flags.FLAGS
flags.DEFINE_string('Flag1', 'Jane Random', 'Your name.')
class Sample:
def a_method(self):
print(FLAGS.name, "is being called in sample2.py.") #sample.py's flags appear to be accessible even though we're not importing from there.
def __init__(self):
self.a_method()
sample3.Sample()
sample3.py
from absl import app
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_string('Flag2', 'This is a sample3.py flag.')
class Sample:
def a_method(self):
print(FLAGS.name, "is being called in sample3.py.") #sample.py's flags appear to be accessible even though we're not importing from there.
def __init__(self):
self.a_method()
当您设置 FLAGS = flags.FLAGS
时,您 importing flags._flagvalues.FLAGS
。
flags._flagvalues.FLAGS
是 _flagvalues
中的 instance of the FlagValues
class defined。
它们都可以访问相同标志的原因是因为它们指的是同一个对象,所以任何文件都可以指相同的标志。
如果你想覆盖它,你可以创建一个新的 FlagValues
实例并通过将其作为参数传递给它添加标志,例如
NEW_FLAGS = flags.FlagValues()
new_flag = flags.DEFINE_string('name', 'default', 'help', flag_values=NEW_FLAGS)
flag_values
的默认参数总是 _flag_values.FLAGS
绳降文档中有如下说法:
When one Python module imports another, it gains access to the other’s flags. (This behavior is implemented by having all modules share a common, global registry object containing all the flag information.)
当一个模块导入另一个模块时——获得对另一个模块标志的访问权限——是否反过来?换句话说,模型 被导入 是否也可以访问导入模型的标志?就行为而言,情况似乎是这样。一般规则是否应该是任何两个具有某种导入连接的 python 文件都可以访问彼此的标志,并且这种情况是可传递的?
以下三个文件展示了我所指的行为。
sample.py
from absl import app
from absl import flags
import sample2
FLAGS = flags.FLAGS
flags.DEFINE_string('name', 'Jane Random', 'Your name.')
def main(argv):
print(FLAGS.name, "is being called in sample.py.")
print(FLAGS.Flag2) #Flag2 is a flag of sample3.py.
sample2.Sample()
if __name__ == '__main__':
app.run(main)
sample2.py:
from absl import app
from absl import flags
import sample3
FLAGS = flags.FLAGS
flags.DEFINE_string('Flag1', 'Jane Random', 'Your name.')
class Sample:
def a_method(self):
print(FLAGS.name, "is being called in sample2.py.") #sample.py's flags appear to be accessible even though we're not importing from there.
def __init__(self):
self.a_method()
sample3.Sample()
sample3.py
from absl import app
from absl import flags
FLAGS = flags.FLAGS
flags.DEFINE_string('Flag2', 'This is a sample3.py flag.')
class Sample:
def a_method(self):
print(FLAGS.name, "is being called in sample3.py.") #sample.py's flags appear to be accessible even though we're not importing from there.
def __init__(self):
self.a_method()
当您设置 FLAGS = flags.FLAGS
时,您 importing flags._flagvalues.FLAGS
。
flags._flagvalues.FLAGS
是 _flagvalues
中的 instance of the FlagValues
class defined。
它们都可以访问相同标志的原因是因为它们指的是同一个对象,所以任何文件都可以指相同的标志。
如果你想覆盖它,你可以创建一个新的 FlagValues
实例并通过将其作为参数传递给它添加标志,例如
NEW_FLAGS = flags.FlagValues()
new_flag = flags.DEFINE_string('name', 'default', 'help', flag_values=NEW_FLAGS)
flag_values
的默认参数总是 _flag_values.FLAGS