【发布时间】:2019-01-26 20:31:56
【问题描述】:
在这个 Python 2.7 代码中,应该用什么代替 GET_MY_COMPILER_FLAGS() ?
from __future__ import print_function
def ReportCompilerFlags():
myFlags = GET_MY_COMPILER_FLAGS() # how?
import __future__
for featureName in __future__.all_feature_names:
thisFlag = getattr(__future__, featureName).compiler_flag
print('File {fileName} has {featureName} turned {status}.'.format(
fileName = __file__,
featureName = featureName,
status = 'ON' if (myFlags & thisFlag) else 'off',
))
ReportCompilerFlags()
期望的输出:
File /path/to/this/script.py has nested_scopes turned off.
File /path/to/this/script.py has generators turned off.
File /path/to/this/script.py has division turned off.
File /path/to/this/script.py has absolute_import turned off.
File /path/to/this/script.py has with_statement turned off.
File /path/to/this/script.py has print_function turned ON.
File /path/to/this/script.py has unicode_literals turned off.
我知道我可以检查 globals() 以查看它是否包含适当命名的符号,而不是查看标志,但显然误报和误报都是可能的。
更新:我更新了标题以从经典的 XY 问题中挖掘出这个问题。要查询语言功能的激活状态,事实证明我应该查看的不仅仅是编译器标志,因为编译器实际上并没有将标志用于已经是强制性的功能。
【问题讨论】:
标签: python python-2.7 python-2.x