Python 内置变量
Posted YM的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 内置变量相关的知识,希望对你有一定的参考价值。
Python 隐藏变量
__doc__ # 表示本文件的注释
__file__ # 表示本文件的路径
__package__ # 导入的py文件所在的文件夹路径
__cached__ # 导入文件的缓存
__name__ #如果是主文件 __name__ = "__main__" 否则等于函数名称
#!/usr/bin/env python # -*- coding:utf-8 -*- """ { ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__package__‘: None, ‘__cached__‘: None, ‘__file__‘: ‘C:/Users/Administrator/PycharmProject/MyProject/myfunc/ceshi.py‘, ‘__doc__‘: None, ‘__name__‘: ‘__main__‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x0000026EC5A58978>, ‘__spec__‘: None} """ if __name__ == ‘__main__‘: print(__file__) print(__doc__) print(vars())
运行结果:
C:/Users/Administrator/PycharmProject/MyProject/myfunc/ceshi.py # __file__ { # __doc__ ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__package__‘: None, ‘__cached__‘: None, ‘__file__‘: ‘C:/Users/Administrator/PycharmProject/MyProject/myfunc/ceshi.py‘, ‘__doc__‘: None, ‘__name__‘: ‘__main__‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x0000026EC5A58978>, ‘__spec__‘: None}
# vars() {‘__cached__‘: None, ‘__package__‘: None, ‘__doc__‘: "\n{\n ‘__builtins__‘: <module ‘builtins‘ (built-in)>, \n ‘__package__‘: None,\n ‘__cached__‘: None,\n ‘__file__‘: ‘C:/Users/Administrator/PycharmProject/MyProject/myfunc/ceshi.py‘, \n ‘__doc__‘: None, \n ‘__name__‘: ‘__main__‘, \n ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x0000026EC5A58978>,\n ‘__spec__‘: None}\n", ‘__builtins__‘: <module ‘builtins‘ (built-in)>, ‘__file__‘: ‘C:/Users/Administrator/PycharmProject/MyProject/myfunc/ceshi.py‘, ‘__loader__‘: <_frozen_importlib_external.SourceFileLoader object at 0x000002354BFB8978>, ‘__spec__‘: None, ‘__name__‘: ‘__main__‘}
以上是关于Python 内置变量的主要内容,如果未能解决你的问题,请参考以下文章