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. Disclaimer 2 This is far from an extensive approach of

    OOP or OOP in Python. Both because time and knowledge limitation :|
  2. How? • OOP: quick intro • Defining objects • Magic

    (special) methods ◦ Protocols • Objects everywhere • Instance and class attributes • Class methods • Extra: mixins considerations 4
  3. 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
  4. 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
  5. 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
  6. 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”
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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