特殊方法的 Python 文档在哪里?(__init__, __new__, __len__, ...)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1418825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where is the Python documentation for the special methods? (__init__, __new__, __len__, ...)
提问by mk12
Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__
, __new__
, __len__
, __add__
)
可以在类中使用的特殊双下划线/dunder 方法的完整列表在哪里?(例如, __init__
, __new__
, __len__
, __add__
)
回答by Martin Geisler
Please take a look at the special method names sectionin the Python language reference.
请查看Python 语言参考中的特殊方法名称部分。
回答by Jonny Buchanan
Dive Into Python has an excellent appendixfor them.
Dive Into Python为他们提供了一个很好的附录。
回答by Justin
If, like me, you want a plain, unadorned list, here it is. I compiled it based on the Python documentation linkfrom the accepted answer.
如果像我一样,你想要一个简单、朴素的清单,这里就是。我根据接受的答案中的Python 文档链接编译了它。
__abs__
__add__
__and__
__call__
__class__
__cmp__
__coerce__
__complex__
__contains__
__del__
__delattr__
__delete__
__delitem__
__delslice__
__dict__
__div__
__divmod__
__eq__
__float__
__floordiv__
__ge__
__get__
__getattr__
__getattribute__
__getitem__
__getslice__
__gt__
__hash__
__hex__
__iadd__
__iand__
__idiv__
__ifloordiv__
__ilshift__
__imod__
__imul__
__index__
__init__
__instancecheck__
__int__
__invert__
__ior__
__ipow__
__irshift__
__isub__
__iter__
__itruediv__
__ixor__
__le__
__len__
__long__
__lshift__
__lt__
__metaclass__
__mod__
__mro__
__mul__
__ne__
__neg__
__new__
__nonzero__
__oct__
__or__
__pos__
__pow__
__radd__
__rand__
__rcmp__
__rdiv__
__rdivmod__
__repr__
__reversed__
__rfloordiv__
__rlshift__
__rmod__
__rmul__
__ror__
__rpow__
__rrshift__
__rshift__
__rsub__
__rtruediv__
__rxor__
__set__
__setattr__
__setitem__
__setslice__
__slots__
__str__
__sub__
__subclasscheck__
__truediv__
__unicode__
__weakref__
__xor__
回答by e-satis
回答by Elazar Leibovich
回答by IcarianComplex
Do this if you prefer reading documentation from a CLI instead of the browser.
如果您更喜欢从 CLI 而不是浏览器阅读文档,请执行此操作。
$ pydoc SPECIALMETHODS
$ pydoc SPECIALMETHODS
回答by Stefan van den Akker
For somebody who is relatively new to Python, and for whom the documentation is often not quite accessible enough (like myself): somebody wrote a nice introductionwith lots of examples on how the special (magic) methods work, how to use them, etc.
对于 Python 相对较新的人,并且文档通常不太容易访问的人(例如我自己):有人写了一篇很好的介绍,其中包含许多关于特殊(魔术)方法如何工作,如何使用它们等的示例.
回答by user2314737
Python's double underscore ("dunder") methods are also known as datamodelmethods because they are at the core of Python's data model, providing a protocol for customizing (overloading) built-in methods. This is the reason why they are listed in the "Data Model"section of the Python's documentation.
Python 的双下划线(“dunder”)方法也称为数据模型方法,因为它们是 Python 数据模型的核心,为自定义(重载)内置方法提供了协议。这就是它们被列在Python 文档的“数据模型”部分的原因。
回答by TheOne
Familiarize yourself with the dir function.
熟悉 dir 函数。