Type conversion methods
Python offers a set of methods responsible for the conversion of built-in data types.
| Function | Magic method | Implementation meaning or purpose |
|---|---|---|
| int() | __int__(self) | conversion to integer type |
| float() | __float__(self) | conversion to float type |
| oct() | __oct__(self) | conversion to string, containing an octal representation |
| hex() | __hex__(self) | conversion to string, containing a hexadecimal representation |
Object introspection
Python offers a set of methods responsible for representing object details using ordinary strings.
| Function | Magic method | Implementation meaning or purpose |
|---|---|---|
| str() | __str__(self) | responsible for handling str() function calls |
| repr() | __repr__(self) | responsible for handling repr() function calls |
| format() | __format__(self, formatstr) | called when new-style string formatting is applied to an object |
| hash() | __hash__(self) | responsible for handling hash() function calls |
| dir() | __dir__(self) | responsible for handling dir() function calls |
| bool() | __nonzero__(self) | responsible for handling bool() function calls |
Object retrospection
Following the topic of object introspection, there are methods responsible for object reflection.
| Function | Magic method | Implementation meaning or purpose |
|---|---|---|
| isinstance(object, class) | __instancecheck__(self, object) | responsible for handling isinstance() function calls |
| issubclass(subclass, class) | __subclasscheck__(self, subclass) | responsible for handling issubclass() function calls |
Object attribute access
Access to object attributes can be controlled via the following magic methods
| Expression example | Magic method | Implementation meaning or purpose |
|---|---|---|
| object.attribute | __getattr__(self, attribute) | responsible for handling access to a non-existing attribute |
| object.attribute | __getattribute__(self, attribute) | responsible for handling access to an existing attribute |
| object.attribute = value | __setattr__(self, attribute, value) | responsible for setting an attribute value |
| del object.attribute | __delattr__(self, attribute) | responsible for deleting an attribute |
Methods allowing access to containers
Containers are any object that holds an arbitrary number of other objects; containers provide a way to access the contained objects and to iterate over them. Container examples: list, dictionary, tuple, and set.
| Expression example | Magic method | Implementation meaning or purpose |
|---|---|---|
| len(container) | __len__(self) | returns the length (number of elements) of the container |
| container[key] | __getitem__(self, key) | responsible for accessing (fetching) an element identified by the key argument |
| container[key] = value | __setitem__(self, key, value) | responsible for setting a value to an element identified by the key argument |
| del container[key] | __delitem__(self, key) | responsible for deleting an element identified by the key argument |
| for element in container | __iter__(self) | returns an iterator for the container |
| item in container | __contains__(self, item) | responds to the question: does the container contain the selected item? |
The list of special methods built-in in Python contains more entities. For more information, refer to https://docs.python.org/3/reference/datamodel.html#special-method-names.