Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Demystifying python magic __methods__

April 28, 2018

Demystifying python magic __methods__

A walk through of python dunder methods and their usages.


April 28, 2018

More Decks by Naren

Other Decks in Programming


  1. WHAT? • Pre-defined methods used to enrich your classes •

    Also known as • Special methods • Dunder methods (Double underscores) • Lets you to emulate the builtin types
  2. YOU KNOW… In [1]: l = ['a', 'b', 'c', ‘d']

    In [2]: l.index('b') Out[2]: 1 In [3]: t = ('a', 'b', 'c', 'd',) In [4]: t.index('b') Out[4]: 1 In [5]: len(l) Out[5]: 4 In [6]: len(t) Out[6]: 4
  3. FROM PYTHON FAQ… Why does Python use methods for some

    functionality (e.g. list.index()) but functions for other (e.g. len(list))? • The major reason is history. • Functions were used for those operations that were generic for a group of types and which were intended to work even for objects that didn’t have methods at all (e.g. tuples). It is also convenient to have a function that can readily be applied to an amorphous collection of objects when you use the functional features of Python (map(), apply() et al). • In fact, implementing len(), max(), min() as a built-in function is actually less code than implementing them as methods for each type. • It’s too late to make such fundamental changes now. The functions have to remain to avoid massive code breakage.
  4. __init__ __new__ __del__ __hash__ __slots__ __dict__ __instancecheck__ __subclasscheck__ __subclasshook__ __enter__

    __exit__ __copy__ __deepcopy__ __getstate__ __reduce__ __reduce_ex__ __getnewargs__ __getattr__ __getattribute__ __setattr__ __delattr__ __dir__ __add__ __sub__ __mul__ __floordiv__ __truediv__ __mod__ __pow__ __lshift__ __rshift__ __and__ __xor__ __or__ __iadd__ __isub__ __imul__ __idiv__ __ifloordiv__ __imod__ __ipow__ __ilshift__ __irshift__ __iand__ __ixor__ __ior__ __neg__ __pos__ __abs__ __invert__ __complex__ __int__
 __long__ __float__ __oct__ __hex__ __lt__ __le__ __eq__ __ne__ __ge__ __gt__
  5. BINARY OPERATORS + object.__add__(self, other) - object.__sub__(self, other) * object.__mul__(self,

    other) // object.__floordiv__(self, other) / object.__truediv__(self, other) % object.__mod__(self, other) ** object.__pow__(self, other[, modulo]) << object.__lshift__(self, other)
  6. BINARY OPERATORS (CONTD.) << object.__lshift__(self, other) >> object.__rshift__(self, other) &

    object.__and__(self, other) ^ object.__xor__(self, other) | object.__or__(self, other)
  7. EXTENDED OPERATORS += object.__iadd__(self, other) -= object.__isub__(self, other) *= object.__imul__(self,

    other) /= object.__idiv__(self, other) //= object.__ifloordiv__(self, other) %= object.__imod__(self, other) <<=object.__ilshift__(self, other) >>=object.__irshift__(self, other)
  8. UNARY OPERATORS - object.__neg__(self) + object.__pos__(self) abs() object.__abs__(self) ~ object.__invert__(self)

    complex() object.__complex__(self) int() object.__int__(self) long()object.__long__(self) float() object.__float__(self) oct() object.__oct__(self) hex() object.__hex__(self)
  9. COMPARISON OPERATORS < object.__lt__(self, other) <= object.__le__(self, other) == object.__eq__(self,

    other) !=object.__ne__(self, other) >= object.__ge__(self, other) > object.__gt__(self, other)
  10. ⚠ CAUTION ⚠ • Over use of magic methods makes

    your code complex to understand