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

Beyond Paradigms (with Python examples)

Beyond Paradigms (with Python examples)

Java is object oriented and Haskell is functional. How about Python?
Is it really OO with free-standing functions and porous encapsulation?
Python has lambdas and closures, but is it functional? Are these
useful questions?

Updated talk presented at North Bay Python, July 29th, 2022.

Luciano Ramalho

August 03, 2023
Tweet

More Decks by Luciano Ramalho

Other Decks in Programming

Transcript

  1. T h e o r y f o r p

    r a c t i c e BEYOND PARADIGMS Understand language features, 
 use the right patterns. Luciano Ramalho @[email protected] North Bay Python 
 July 29th, 2023
  2. 4

  3. GCD IN PYTHON Imperative style Functional style 11 Bad fi

    t for Python: no tail-call optimisation
  4. DEWEY'S CLASSIFICATION: A TIDY HIERARCHY 000 Computer science, information and

    general works 100 Philosophy and psychology 200 Religion 300 Social sciences 400 Language 500 Pure science 600 Technology 700 Arts and recreation 800 Literature 900 History and geography 14
  5. A TIDY HIERARCHY 500 Natural sciences and mathematics 510 Mathematics

    516 Geometry 516.3 Analytic geometries 516.37 Metric differential geometries 516.375 Finsler geometry 15
  6. A TIDY HIERARCHY? 200 Religion 210 Philosophy and theory of

    religion 220 The Bible 230 Christianity 240 Christian practice and observance 250 Christian orders and local church 260 Social and ecclesiastical theology 270 History of Christianity 280 Christian denominations 290 Other religions 16
  7. A TIDY HIERARCHY? 200 Religion ... 290 Other religions 291

    No longer used — formerly "Comparative religion" 292 Classical religion (Greek and Roman religion) 293 Germanic religion 294 Religions of Indic origin 295 Zoroastrianism (Mazdaism, Parseeism) 296 Judaism 297 Islam, Bábism and Baháʼí Faith 298 No longer used — formerly "Mormonism" 299 Religions not provided for elsewhere 300 Social Sciences 17
  8. 19

  9. 20

  10. GOF: CLASSIC BOOK BY THE “GANG OF FOUR” Design Patterns:

    Elements of Reusable Object-Oriented Software (1995) Erich Gamma 
 Richard Helm 
 Ralph Johnson 
 John Vlissides 33
  11. NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph

    Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 34
  12. NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph

    Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 35
  13. NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph

    Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 36
  14. 37

  15. 38

  16. 39

  17. 40

  18. THE ITERATOR FROM THE GANG OF FOUR Design Patterns 


    Gamma, Helm, Johnson & Vlissides 
 ©1994 Addison-Wesley 42
  19. THE FOR LOOP MACHINERY • In Python, the for loop,

    automatically: •Obtains an iterator from the iterable •Repeatedly invokes next() on the iterator, 
 retrieving one item at a time •Assigns the item to the loop variable(s) 44 for item in an_iterable: process(item) for item in an_iterable: process(item) •Terminates when a call to next() raises StopIteration.
  20. ITERABLE VERSUS ITERATOR 45 • iterable: implements Iterable interface (__iter__

    method) •__iter__ method returns an Iterator 
 • iterator: implements Iterator interface (__next__ method) •__next__ method returns next item in series and •raises StopIteration to signal end of the series Python iterators are also iterable!
  21. AN ITERABLE TRAIN An instance of Train can be iterated,

    car by car 46 >>> t = Train(3) >>> for car in t: ... print(car) car #1 car #2 car #3 >>>
  22. CLASSIC ITERATOR IMPLEMENTATION The pattern as described by Gamma et.

    al. 47 class Train: def __init__(self, cars): self.cars = cars def __iter__(self): return TrainIterator(self.cars) class TrainIterator: def __init__(self, cars): self.next = 0 self.last = cars - 1 def __next__(self): if self.next <= self.last: self.next += 1 return 'car #%s' % (self.next) else: raise StopIteration() >>> t = Train(4) >>> for car in t: ... print(car) car #1 car #2 car #3 car #4
  23. BARBARA LISKOV'S CLU LANGUAGE 48 CLU Reference Manual — B.

    Liskov et. al. — © 1981 Springer-Verlag — also available online from MIT: http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-225.pdf © 2010 Kenneth C. Zirkel — CC-BY-SA
  24. 49

  25. 50

  26. 51

  27. ITERATION IN CLU CLU also introduced true iterators with yield

    and a generic for/ in statement. 52 year: 1975 CLU Reference Manual, p. 2 B. Liskov et. al. — © 1981 Springer-Verlag
  28. ITERABLE OBJECTS: THE KEY TO FOREACH • Python, Java &

    CLU let programmers de fi ne iterable objects 
 
 
 
 
 
 
 • Some languages don't o ff er this fl exibility • C has no concept of iterables • In Go, only some built-in types are iterable and can be used with foreach (written as the for … range special form) 53 for item in an_iterable: process(item) for item in an_iterable: process(item)
  29. ITERABLE TRAIN WITH A GENERATOR METHOD The Iterator pattern as

    a language feature: 54 class Train: def __init__(self, cars): self.cars = cars def __iter__(self): for i in range(self.cars): yield 'car #%s' % (i+1) Train is now iterable because __iter__ returns a generator! >>> t = Train(3) >>> it = iter(t) >>> it 
 <generator object __iter__ at 0x…> 
 >>> next(it), next(it), next(it) 
 ('car #1', 'car #2', 'car #3')
  30. COMPARE: CLASSIC ITERATOR × GENERATOR METHOD The classic Iterator recipe

    is obsolete in Python since v.2.2 (2001) 55 class Train: def __init__(self, cars): self.cars = cars def __iter__(self): for i in range(self.cars): yield 'car #%s' % (i+1) class Train: def __init__(self, cars): self.cars = cars def __iter__(self): return IteratorTrem(self.cars) class TrainIterator: def __init__(self, cars): self.next = 0 self.last = cars - 1 def __next__(self): if self.next <= self.last: self.next += 1 return 'car #%s' % (self.next) else: raise StopIteration() Generator function handles the state of the iteration
  31. SAMPLE FEATURES ✖ LANGUAGES 57 Common Lisp C Java Python

    Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural
  32. SAMPLE FEATURES ✖ LANGUAGES 58 Common Lisp C Java Python

    Go First-class functions ✔ ∗ ✔ ✔ ✔ First class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural Functions as objects Classes as objects
  33. SAMPLE FEATURES ✖ LANGUAGES 59 Common Lisp C Java Python

    Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural
  34. SAMPLE FEATURES ✖ LANGUAGES 60 Common Lisp C Java Python

    Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural
  35. SAMPLE FEATURES ✖ LANGUAGES 61 Common Lisp C Java Python

    Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural
  36. SAMPLE FEATURES ✖ LANGUAGES 62 Common Lisp C Java Python

    Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ * Variable model reference value* value and reference reference value* and reference* Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural
  37. DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY 69 Instance of

    Strategy 
 is given to Order constructor
  38. VARIATIONS OF STRATEGY IN PYTHON Classic implementation using ABC First-class

    function implementation Parameterised closure implementation Parameterised callable implementation 71
  39. 81

  40. 82

  41. 83

  42. 84

  43. 85

  44. 86

  45. 87

  46. FEATURES ARE THE BEST GUIDE… 91 …to decide whether a

    particular pattern or implementation makes the most of the language.
  47. WHY LEARN THE FUNDAMENTALS* Learn new languages faster Leverage language

    features Choose among alternative implementations Make sensible use of design patterns Debug hard problems Emulate missing features when they are helpful 92 Inspired by Programming Language Pragmatics Michael L. Scott *
  48. Luciano Ramalho [email protected] 
 Fediverse: @[email protected] 
 PFKAT: @ramalhoorg Repo:

    github.com/ramalho/beyond-paradigms Slides: speakerdeck.com/ramalho THANKS !