Magic attributes and methods
Magic attributes and methods
Builtin class attributes:
__name__
__doc__
__module__
__dict__
__class__
Magic methods
__<method>__
__init__
Initializing instance
__new__
Instance constructor
__repr__
Representation of an object
__str__
String representation
__del__
Deletion of object(called when del used and count to obj is 0)
Instance constructor
__new__()
First method for creating new object
It is static method
Invoked before
__init__()
, need to return objectWorks with
cls
(class object, as instance is not created yet)Old style class don't have this method!
Instance initializer
__init__()
It is not a constructor (there is
__new__()
for that)__init__()
takes an already created object and fills required attributes according to declared logic first arg isself
Doesn't return anything (it returns
None
), just assigning attributes to an object.
Representations
__str__(self)
String representation – for humans
Used when
str(obj)
orprint(obj)
__repr__(self)
Representation for "machines"
Ideal case:
eval(repr(obj)) --> obj
Used when calling
repr(obj)
or justobj
in Python session
Hash
__hash__(self)
Called by hashed collections:
set
frozenset
dict
In simplest case as for instances it is based on
id(self)
so all objects are different
Bases
__bases__
(attribute)
Tuple (possibly empty or a singleton) containing the base classes, in the order of their occurrence in the base class list
Docstring
__doc__
(attribute)
Class’s documentation string, or None if undefined.
Module
__module__
(attribute)
Module name in which the class is defined. This attribute is
"__main__"
in interactive mode.
Boolean value
__bool__(self)
(__nonzero__
for Python 2)
Used when
bool(obj)
By default - True
Comparison
__eq__(self, other)
Return
True
if objects are equal
__gt__(self, other)
Return
True
ifself > other
Other methods:
__ne__(self, other) # !=
__ge__(self, other) # >=
__lt__(self, other) # <
__le__(self, other) # <=
It is really a pain to fill all of those!
Battery: functools.total_ordering
Iterator protocol
__iter__(self)
Returns iterator for given object (or
self
if it is iterator)
__next__(self)
(next(self)
for Python 2.x)
Return next value for iterator-like object
Call
Object can be callable
(function-like object).
__call__(self, *args)
This is very useful to call the method which used very often.
Slots and __dict__
__dict__
All instance's attributes are hold in __dict__
attribute which is a dictionary with keys as names bound to the instance.
__slots__
is a list with instance attributes that can be assigned
This is needed to suppress automatic creation of __dict__
- this can be useful when we have a lot of simple objects and their dictionaries are taking too much space. Also slots are limiting attributes that can be assigned to an object.
__slots__
are meaninless when the class is inherited from the one which doesn't have __slots__
.
Last updated