特殊方法的Python文档在哪里? (__init__,__new__,__len__,...)
问题描述:
可以在类中使用的特殊双下划线/ dunder方法的完整列表在哪里? (例如,__init__
,__new__
,__len__
,__add__
)特殊方法的Python文档在哪里? (__init__,__new__,__len__,...)
答
如果像我一样,你想要一个简单的,朴实无华的列表,在这里。我根据接受的答案编译了Python documentation link。
__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__
答
如果您喜欢从CLI而不是浏览器阅读文档,请执行此操作。
$ pydoc SPECIALMETHODS
答
Python的双下划线(“dunder”)的方法也被称为数据模型方法,因为它们是在Python的数据模型的核心,提供用于定制(重载)内置方法的协议。 这就是它们在Python文档的"Data Model"部分列出的原因。
您无法找到Python文档的位置? – 2009-09-13 23:16:04
@ Mk12:标签与问题无关。请停止回复 – SilentGhost 2009-09-14 18:16:22
@ S.Lott也许OP根本找不到相关章节?如果你没有建设性的东西来添加... – 2012-01-23 02:15:03