I see some strange but useful double underscored attribute in Python, such as:
__module__
__init__
__str__
__class__
__repr__
...
They seem to be some special attributes. What's the canonical name for them?
I see some strange but useful double underscored attribute in Python, such as:
__module__
__init__
__str__
__class__
__repr__
...
They seem to be some special attributes. What's the canonical name for them?
They are called Special Methods.
Python is a Duck Typed Language and many of the user-facing features of the language are implemented in "protocols" implemented by these special methods.
See: http://docs.python.org/release/2.5.2/ref/specialnames.html
As an Example:
To mimic comparison of arbitrary objects you implement the following two methods in your class:
__lt__
__eq__
Per "Naming conventions" section of PEP-8
__double_leading_and_trailing_underscore__
: "magic" objects or attributes that live in user-controlled namespaces. E.g.__init__
,__import__
or__file__
. Never invent such names; only use them as documented.