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

Pythonic OOP pt. 1

Caio Carrara
February 19, 2020

Pythonic OOP pt. 1

This is a talk about some object-oriented programming characteristics in Python language.

Caio Carrara

February 19, 2020
Tweet

More Decks by Caio Carrara

Other Decks in Programming

Transcript

  1. Pythonic OOP pt. 1
    Caio Carrara
    1

    View full-size slide

  2. Disclaimer
    2
    This is far from an
    extensive approach of OOP
    or OOP in Python. Both
    because time and knowledge
    limitation :|

    View full-size slide

  3. How?
    ● OOP: quick intro
    ● Defining objects
    ● Magic (special) methods
    ○ Protocols
    ● Objects everywhere
    ● Instance and class attributes
    ● Class methods
    ● Extra: mixins considerations
    4

    View full-size slide

  4. Object-oriented programming
    5

    View full-size slide

  5. From Alan Kay - pioneer in OOP
    “I thought of objects being like biological cells and/or
    individual computers on a network, only able to communicate
    with messages (...) OOP to me means only messaging, local
    retention and protection and hiding of state-process”
    https://userpage.fu-berlin.de/~ram/pub/pub_jf47ht81Ht/doc_kay_oop_en 6

    View full-size slide

  6. Python official HOW TO
    “Objects are little capsules containing some internal state
    along with a collection of method calls that let you modify
    this state, and programs consist of making the right set of
    state changes.”
    https://docs.python.org/3/howto/functional.html 7

    View full-size slide

  7. Defining objects
    8

    View full-size slide

  8. Defining objects
    9

    View full-size slide

  9. Defining objects
    10

    View full-size slide

  10. Defining objects
    11

    View full-size slide

  11. Defining objects
    12

    View full-size slide

  12. Defining objects
    13

    View full-size slide

  13. Defining objects
    14

    View full-size slide

  14. Defining objects
    15

    View full-size slide

  15. Defining objects
    16

    View full-size slide

  16. Defining objects
    17

    View full-size slide

  17. *-* Magic methods *-*
    18

    View full-size slide

  18. Python special method names (magic, dunder)
    19
    ● Methods with special names: double underscore before and
    after the method name itself:
    ○ __new__, __init__, __len__, __del__, __repr__, __str__ …
    ● When writing Python code we shouldn’t call special
    methods explicitly. Python interpreter does that

    View full-size slide

  19. Python special method names (magic, dunder)
    20

    View full-size slide

  20. Python special method names (magic, dunder)
    21

    View full-size slide

  21. Python special method names (magic, dunder)
    22
    https://docs.python.org/3/reference/datamodel.html#special-method-names

    View full-size slide

  22. Magic methods: protocols
    23

    View full-size slide

  23. Python typing
    24
    ● Dynamic typing: Python variable values has a type only on
    runtime. The variable values have a type and not the
    variable itself.
    ● Strongly typed: Python restricts how the different types
    can interact with each other
    ● Duck typing: “if it walks like a duck and it quacks like
    a duck, then it must be a duck”

    View full-size slide

  24. Python special method names (magic, dunder)
    25
    ● How is it possible to create a Collections-like object?
    ○ Inheriting?
    ○ Implementing an Interface?
    ○ Making it “quack” (behave) like a Collection

    View full-size slide

  25. Python special method names (magic, dunder)
    26
    ● How is it possible to create a Collections-like object?
    ○ Inheriting?
    ○ Implementing an Interface?
    ○ Making it “quack” (behave) like a Collection: following a protocol

    View full-size slide

  26. Python special method names (magic, dunder)
    27

    View full-size slide

  27. Python special method names (magic, dunder)
    28

    View full-size slide

  28. Objects everywhere
    29

    View full-size slide

  29. Objects everywhere...
    https://docs.python.org/3/reference/datamodel.html 30

    View full-size slide

  30. Objects everywhere...
    https://docs.python.org/3/library/functions.html#type 31

    View full-size slide

  31. Objects everywhere...
    32

    View full-size slide

  32. Objects everywhere...
    33

    View full-size slide

  33. Objects everywhere...
    34

    View full-size slide

  34. Objects everywhere...
    35

    View full-size slide

  35. Instance and class attributes
    36

    View full-size slide

  36. Instance and class attributes
    37

    View full-size slide

  37. Instance and class attributes
    38

    View full-size slide

  38. Class methods
    39
    https://caiocarrara.com.br/blog/python-classmethod-o-que-e-e-quando-usar.html (PT-BR)

    View full-size slide

  39. Class methods
    40

    View full-size slide

  40. Class methods
    41

    View full-size slide

  41. Class methods
    42

    View full-size slide

  42. Class methods - when to use them?
    ● In Python everything is an object
    ● Objects have state and behavior
    ● Since Classes are objects as well, class methods should
    be used when it’s needed to extend a class object
    behavior
    43

    View full-size slide

  43. Class methods - when to use them?
    “Classes are callable. These objects normally act as
    factories for new instances of themselves, but variations
    are possible for class types that override __new__(). The
    arguments of the call are passed to __new__() and, in the
    typical case, to __init__() to initialize the new instance.”
    44
    https://docs.python.org/3/reference/datamodel.html

    View full-size slide

  44. Class methods - when to use them?
    “Classes are callable. These objects normally act as
    factories for new instances of themselves, but variations
    are possible for class types that override __new__(). The
    arguments of the call are passed to __new__() and, in the
    typical case, to __init__() to initialize the new instance.”
    45
    https://docs.python.org/3/reference/datamodel.html

    View full-size slide

  45. Class methods - when to use them?
    ● Therefore we could consider the class methods as an way
    to extend the responsibility of a class object to create
    new instances of themselves
    46

    View full-size slide

  46. Class methods
    47

    View full-size slide

  47. Extra: mixins considerations
    48

    View full-size slide

  48. Mixins considerations
    49
    ● The name was inspired by Steve's Ice Cream Parlor
    ● The owner of the ice cream shop offered a basic flavor of
    ice cream (vanilla, chocolate, etc) and blended in a
    combination of extra items (nuts, cookies, fudge, etc.)
    and called the item a "mix-in"
    https://en.wikipedia.org/wiki/Mixin

    View full-size slide

  49. Mixins considerations
    50
    ● Mixins are not the place to implement business logic
    ● Mixins being used by only one class is a code smell
    ● Ideally someone could remove without too much pain the
    mixin dependency without breaking the core functionality
    of an object (loose coupling)
    ● Mixins are never directly instantiated

    View full-size slide

  50. Mixins considerations: anti-pattern
    51

    View full-size slide

  51. Mixins considerations: Django example
    52

    View full-size slide

  52. Mixins considerations: Django example
    53

    View full-size slide

  53. Thanks!
    Questions?
    54

    View full-size slide