Where is a complete list of the special double-underscore/dunder methods that can be used in classes? (e.g., __init__
, __new__
, __len__
, __add__
)
- 28,846
- 11
- 78
- 104
- 24,644
- 29
- 92
- 132
-
@Mk12: tags are about questions not about askers. Please, stop reverting – SilentGhost Sep 14 '09 at 18:16
-
I'm trying to figure out what the question means. These these are well-covered in the Python documentation. Since these things are well-documented, I'm trying to understand what the question means. – S.Lott Jan 23 '12 at 03:46
9 Answers
Please take a look at the special method names section in the Python language reference.
- 69,865
- 23
- 162
- 224
If, like me, you want a plain, unadorned list, here it is. I compiled it based on the Python documentation link from the accepted answer.
__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__
- 6,251
- 3
- 33
- 52
Dive Into Python has an excellent appendix for them.
- 41,771
- 21
- 173
- 252
- 58,371
- 16
- 137
- 146
Do this if you prefer reading documentation from a CLI instead of the browser.
$ pydoc SPECIALMETHODS
- 173
- 1
- 5
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 introduction with lots of examples on how the special (magic) methods work, how to use them, etc.
- 55,025
- 29
- 167
- 231
- 5,714
- 7
- 38
- 57
Python's double underscore ("dunder") methods are also known as datamodel methods 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.
- 21,279
- 16
- 81
- 95
Familiarize yourself with the dir function.
- 9,685
- 19
- 74
- 111
-
1but that would only do what I wanted if a the class passed to it implemented all of the special methods. – mk12 Sep 13 '09 at 22:55