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 theinoperatorif 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.nameis 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 thewithstatement__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