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

Demystifying python magic __methods__

Naren
April 28, 2018

Demystifying python magic __methods__

A walk through of python dunder methods and their usages.

Naren

April 28, 2018
Tweet

More Decks by Naren

Other Decks in Programming

Transcript

  1. DEMYSTIFYING PYTHON
    MAGIC __METHODS__
    Naren

    View Slide

  2. WHO AM I ?
    Naren
    Software Consultant
    @DudeWhoCode


    Cycling | Travelling | Blogging

    View Slide

  3. MAGIC METHODS
    • What ?
    • Why ?
    • How ?

    View Slide

  4. MOST FAMILIAR MAGIC METHOD ?

    View Slide

  5. MOST FAMILIAR MAGIC METHOD ?
    __init__( )

    View Slide

  6. 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

    View Slide

  7. 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

    View Slide

  8. 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.

    View Slide

  9. HOW ?

    View Slide

  10. DEMO

    View Slide

  11. View Slide

  12. __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__

    View Slide

  13. 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)

    View Slide

  14. BINARY OPERATORS (CONTD.)
    << object.__lshift__(self, other)
    >> object.__rshift__(self, other)
    & object.__and__(self, other)
    ^ object.__xor__(self, other)
    | object.__or__(self, other)

    View Slide

  15. 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)

    View Slide

  16. EXTENDED OPERATORS
    (CONTD.)
    |=object.__ior__(self, other)
    &= object.__iand__(self, other)
    ^= object.__ixor__(self, other)
    **= object.__ipow__(self, other[, modulo])

    View Slide

  17. 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)

    View Slide

  18. 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)

    View Slide

  19. ⚠ CAUTION ⚠
    • Over use of magic methods makes your code
    complex to understand

    View Slide

  20. THANK YOU,
    NAREN
    @DudeWhoCode
    Slides:

    www.dudewho.codes/talks

    View Slide