查找所有双下划线变量的列表

发布于 2024-12-27 21:34:37 字数 932 浏览 5 评论 0原文

在哪里可以找到 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 中。这些是唯一保留的双下划线变量吗?还有吗?有什么地方可以拿到清单吗?

相关:

Where can I find a list of all double-underscore variables that are commonly used in 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()

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?

Things like __init__, __name__, and __doc__ are pretty much built into Python. Are those the only reserved double-underscore variables? Are there any more? Is there some place I can get a list?

Related:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不羁少年 2025-01-03 21:34:37

如果您想查看魔术名称是否已记录,请转到 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__'

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

That produces:

'__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__'
梦过后 2025-01-03 21:34:37

Python 使用的完整列表在 Python 语言参考第 3 节“数据模型”。其他每个模块都是非标准的或由第三方模块使用,并单独记录。

The complete list used by Python is given in the Python Language Reference section 3, "Data model". Every other one is non-standard or used by third-party modules and is documented separately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文