Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
据我所知(如果我错了,请纠正我)Python解释器在启动时创建了一些值。
有没有办法找出这些值是什么?
检查globals()、locals()和__builtins__。
globals()
locals()
__builtins__
例子:
>>> '__name__' in locals() True >>> locals()['__name__'] '__main__'
而如果你在交互式解释器中,例如,__file__不存在。
__file__
从脚本运行:
print('__file__' in locals())
印刷:
True
在交互式解释器中运行:
>>> '__file__' in locals() False