相关:What is the common header format of Python files?
在哪里可以找到 Python 中常用的所有双下划线变量的列表?
在 Python 中,以双下划线开头和结尾的变量通常用于存储元数据或内置于系统中。例如,
#!/usr/bin/env python
__author__ = 'Michael0x2a'
__license__ = 'GPL'
class Test(object):
def __init__(self):
print 'Hello World!'
if __name__ == '__main__':
t = Test()
我很确定 __author__
和 __license__
是众所周知的。还有哪些其他双下划线元数据变量?有没有我可以在某处查看的完整列表?我可以自己制定,还是有一堆已经成为我应该使用的事实上的标准?
__init__
、__name__
和 __doc__
之类的东西几乎都内置在 Python 中。这些是唯一保留的双下划线变量吗?还有吗?有什么地方可以得到一份 list 吗?
最佳答案
如果您想查看是否记录了魔术名称,请转到 Lib 目录并运行:
egrep -oh '__[A-Za-z_][A-Za-z_0-9]*__' *.py | sort | uniq
产生:
'__all__'
'__args__'
'__author__'
'__bases__'
'__builtin__'
'__builtins__'
'__cached__'
'__call__'
'__class__'
'__copy__'
'__credits__'
'__date__'
'__decimal_context__'
'__deepcopy__'
'__dict__'
'__doc__'
'__exception__'
'__file__'
'__flags__'
'__ge__'
'__getinitargs__'
'__getstate__'
'__gt__'
'__import__'
'__importer__'
'__init__'
'__ispkg__'
'__iter__'
'__le__'
'__len__'
'__loader__'
'__lt__'
'__main__'
'__module__'
'__mro__'
'__name__'
'__package__'
'__path__'
'__pkgdir__'
'__return__'
'__safe_for_unpickling__'
'__setstate__'
'__slots__'
'__temp__'
'__test__'
'__version__'
关于python - 查找所有双下划线变量的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8920341/