In Oop
In OOP Code
items can have their operations,
__add__
==( + )
have their operations of arithmatic assignments__iadd__
==( += )
and If one of those methods does not support the operation with the supplied arguments, it should returnNotImplemented
==__radd__
.
__add__
==( + )
__add____sub__
==( - )
__mul__
==( * )
__matmul__
==( @ )
__truediv__
==( \ )
__floordiv__
==( \\ )
__mod__
==( % )
__divmod__
==divmod()
__pow__
==( ** )
__lshift__
==( << )
__rshift__
==( >> )
__and__
==( & )
__xor__
==( ^ )
__or__
==( | )
__lt__
==( < )
__le__
==( <= )
__eq__
==( == )
__ne__
==( != )
__gt__
==( > )
__ge__
==( >= )
__neg__
==( - )
__pos__
==( + )
__abs__
==abs()
__invert__
==( ~ )
__complex__
==complex()
__int__
==int()
__float__
==float()
__index__
https://docs.python.org/3/reference/datamodel.html#object.__index____round__
==round()
__trunc__
==math.trunc()
__floor__
==math.floor()
__ceil__
==math.ceil()
- Dictionary Dunders
__setitem__
When assigning values in a dictionary__getitem__
Executed when we run thedict['key']
type of operation__delitem__
ran when deleting a dictionary valuedel dict['key']
__contains__
Executed when we run thein
operatorif item in class_instance:
__len__
Is called if we ran alen()
operation on our class instance
__call__
ifinstance = Class()
then__call__
is ran when we do:instance()
right after__init__
init function is run as soon as a class instance is generated. It's the constructor.__post_init__
used in Data Classes for after__init__
as thats overwritten by data class__getattr__
Called when the default attribute access fails with an AttributeError__getattribute__
Called unconditionally to implement attribute accesses for instances of the class.- If the class also defines
__getattr__()
, the latter will not be called unless__getattribute__()
either calls it explicitly or raises an AttributeError.
- If the class also defines
__setattr__
Called when an attribute assignment is attempted.- This is called instead of the normal mechanism (i.e. store the value in the instance dictionary).
__delattr__
Like__setattr__()
but for attribute deletion instead of assignment.- This should only be implemented if
del obj.name
is meaningful for the object.
- This should only be implemented if
__dir__
Called whendir()
is called on the object. A sequence must be returned.- dir() converts the returned sequence to a list and sorts it.
__format__
when the class is being formatted in an fstring for with.format()
__bool__
https://docs.python.org/3/reference/datamodel.html#object.__bool____del__
The destructor of the class and what runs when attempting todel instance
__bytes__
Computes a byte-string representation of an object. This should return a bytes object.__format__
produces a “formatted” string representation of an object.__hash__
https://docs.python.org/3/reference/datamodel.html#object.__hash____repr__
Returns a representation of an object instance as a string __repr____str__
when not defined it calls__repr__
by default to represent the instance as a string __str____unicode__
New preferred method compared to__str__
in python 2- Python 3 uses unicode and therefore defaults to the
__str__
method
- Python 3 uses unicode and therefore defaults to the
__enter__
Enter the runtime context related to this object using thewith
statement__exit__
Exit the runtime context related to this object__reversed__
not used with slice[::-1]
but rather withreversed()
__slots__
https://medium.com/@stephenjayakar/a-quick-dive-into-pythons-slots-72cdc2d334e
Children