Pythonic OOP pt. 1

Pythonic OOP pt. 1

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

F6d5a605df582ab9ea419ebef9f400b7?s=128

Caio Carrara

February 19, 2020
Tweet

Transcript

  1. Pythonic OOP pt. 1 Caio Carrara 1

  2. Disclaimer 2 This is far from an extensive approach of

    OOP or OOP in Python. Both because time and knowledge limitation :|
  3. Why? 3

  4. How? • OOP: quick intro • Defining objects • Magic

    (special) methods ◦ Protocols • Objects everywhere • Instance and class attributes • Class methods • Extra: mixins considerations 4
  5. Object-oriented programming 5

  6. 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
  7. 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
  8. Defining objects 8

  9. Defining objects 9

  10. Defining objects 10

  11. Defining objects 11

  12. Defining objects 12

  13. Defining objects 13

  14. Defining objects 14

  15. Defining objects 15

  16. Defining objects 16

  17. Defining objects 17

  18. *-* Magic methods *-* 18

  19. 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
  20. Python special method names (magic, dunder) 20

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

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

  23. Magic methods: protocols 23

  24. 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”
  25. 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
  26. 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
  27. Python special method names (magic, dunder) 27

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

  29. Objects everywhere 29

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

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

  32. Objects everywhere... 32

  33. Objects everywhere... 33

  34. Objects everywhere... 34

  35. Objects everywhere... 35

  36. Instance and class attributes 36

  37. Instance and class attributes 37

  38. Instance and class attributes 38

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

  40. Class methods 40

  41. Class methods 41

  42. Class methods 42

  43. 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
  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.” 44 https://docs.python.org/3/reference/datamodel.html
  45. 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
  46. 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
  47. Class methods 47

  48. Extra: mixins considerations 48

  49. 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
  50. 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
  51. Mixins considerations: anti-pattern 51

  52. Mixins considerations: Django example 52

  53. Mixins considerations: Django example 53

  54. Thanks! Questions? 54