查找所有双下划线变量的列表? [英] Finding a list of all double-underscore variables?
问题描述
我在哪里可以找到Python中常用的所有双下划线变量/关键字的列表?
在Python中,以双下划线开头和结尾的变量通常用于存储元数据或内置于系统中.例如,
In Python, variables starting and ending with double underscores are typically to store metadata or are built into the system. For example,
#!/usr/bin/env python
__author__ = 'Michael0x2a'
__license__ = 'GPL'
class Test(object):
def __init__(self):
print 'Hello World!'
if __name__ == '__main__':
t = Test()
我敢肯定__author__
和__license__
是众所周知的.还有哪些其他双下划线元数据变量?我可以在某处查看完整的清单吗?我可以自己制作一个,还是一堆已经成为我应该使用的实际标准?
I'm pretty certain __author__
and __license__
are pretty well known. What other double-underscore metadata variables are there? Is there a comprehensive list I can check somewhere? Can I just make up my own, or are there a bunch of ones that have become de-facto standards that I should use?
__init__
,__name__
和__doc__
之类的东西几乎都内置在Python中.那是仅有的两个保留的双下划线关键字吗?还有吗?我可以在哪里找到清单?
Things like __init__
, __name__
, and __doc__
are pretty much built into Python. Are those the only two reserved double-underscore keywords? Are there any more? Is there some place I can get a list?
我正在浏览,遇到了另一个问题,该问题链接到
I was browsing and encountered another question that linked to a mindmap of a bunch of double-underscore variables.
推荐答案
如果要查看魔术名称(无论是否有文献记载),请转到Lib目录并运行:
If you want to see magic names whether documented or not, go to the Lib directory and run:
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__'
这篇关于查找所有双下划线变量的列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!