在python3.6 shell中输入时dir
会产生以下内容:
>>> dir()
['__annotations__', '__builtins__', '__doc__', '__loader__', '__name__', '__package__', '__spec__']
__builtins__
产生所有内置的python方法,例如此处描述的方法,并且__name__
将(总是?)为__main__
。其余的呢:当(1)在python解释器中填充时;或(2)运行脚本:如果是,什么时候?
这是运行名为的python脚本的示例temp.py
:
if __name__ == '__main__':
print (dir())
print ("__annotations__: %s" % __annotations__)
print ("__builtins__: %s" % __builtins__)
print ("__cached__: %s" % __cached__)
print ("__doc__: %s" % __doc__)
print ("__file__: %s" % __file__)
print ("__name__: %s" % __name__)
print ("__package__: %s" % __package__)
print ("__spec__: %s" % __spec__)
运行它:
$ python temp.py
['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__']
__annotations__: {}
__builtins__: <module 'builtins' (built-in)> # <== always populated
__cached__: None
__doc__: None
__file__: temp.py # <== populated if running from a file/script
__name__: __main__ # <== populated (always with main?)
__package__: None
如何/时__annotation__
,__cached__
,__doc__
,__package__
填充?而且是__name__
不是__main__
?
__name__
仅__main__
在正在运行的脚本中。它包含在其上访问模块的标准名称:
>>> __name__
'__main__'
>>> from logging import config
>>> config.__name__
'logging.config'
__cached__
并且__package__
是与导入相关的模块属性。
__doc__
保存当前模块或函数的文档字符串。
__annotations__
保存全局变量的注释。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句