$30 off During Our Annual Pro Sale. View Details »

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

    View Slide

  2. LANGUAGES AND PROGRAMMING PARADIGMS
    2
    Functional
    Imperative


    (or is it procedural?)
    Object

    Oriented

    View Slide

  3. PARADIGMS
    Programming language categories
    3

    View Slide

  4. 4

    View Slide

  5. CALCULATOR PROGRAMMING WAS IMPERATIVE (1976)
    5
    HP-25 TI 58C

    View Slide

  6. HP-25 CALCULATOR PROGRAMMING LANGUAGE
    6

    View Slide

  7. A SURVEY-STYLE PROGRAMMING LANGUAGES BOOK
    Programming
    Language
    Pragmatics,

    4th edition (2015)


    Michael L. Scott


    7

    View Slide

  8. GCD ASM X86
    Greatest

    common divisor
    in x86 Assembly


    (Scott, 2015)
    8

    View Slide

  9. GCD IN C, OCAML AND PROLOG
    9

    View Slide

  10. GCD IN PYTHON
    Imperative style


    Functional style
    10

    View Slide

  11. GCD IN PYTHON
    Imperative style


    Functional style
    11
    Bad
    fi
    t for Python:
    no tail-call
    optimisation

    View Slide

  12. THE PROBLEM

    WITH CATEGORIES
    Ontologies are so 1900’s
    12

    View Slide

  13. DEWEY DECIMAL
    CLASSIFICATION
    DDC 23 (2013 edition)

    13

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  18. CELESTIAL EMPORIUM
    By Jorge Luis Borges (1899-1986)
    18

    View Slide

  19. 19

    View Slide

  20. 20

    View Slide

  21. BETTER CATEGORIES?
    21

    View Slide

  22. A CLASSIFICATION BASED ON HARD FACTS
    22

    View Slide

  23. A CLASSIFICATION BASED ON HARD FACTS?
    23
    “Noble” gases!?

    View Slide

  24. LANGUAGE CATEGORIES (1)
    24

    View Slide

  25. LANGUAGE CATEGORIES (2)
    25

    View Slide

  26. LANGUAGE CATEGORIES (3)
    26

    View Slide

  27. LANGUAGE CATEGORIES (4)
    27

    View Slide

  28. ONE CLASSIFICATION
    28
    Programming
    Language
    Pragmatics,

    4th edition (2015)


    Michael L. Scott


    View Slide

  29. ONE CLASSIFICATION (ZOOM)
    29

    View Slide

  30. ONE CLASSIFICATION (ZOOM)
    30
    ???

    View Slide

  31. “Ontology is overrated.”


    Clay Shirky
    31

    View Slide

  32. DESIGN PATTERNS
    When languages fall short
    32

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  37. 37

    View Slide

  38. 38

    View Slide

  39. 39

    View Slide

  40. 40

    View Slide

  41. THE ITERATOR
    PATTERN
    The classic recipe
    41

    View Slide

  42. THE ITERATOR FROM THE GANG OF FOUR
    Design Patterns

    Gamma, Helm, Johnson & Vlissides

    ©1994 Addison-Wesley
    42

    View Slide

  43. 43
    Head First Design
    Patterns Poster

    O'Reilly

    ISBN 0-596-10214-3

    View Slide

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

    View Slide

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

    View Slide

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


    >>>

    View Slide

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

    View Slide

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

    View Slide

  49. 49

    View Slide

  50. 50

    View Slide

  51. 51

    View Slide

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

    View Slide

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

    View Slide

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



    >>> next(it), next(it), next(it)

    ('car #1', 'car #2', 'car #3')

    View Slide

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

    View Slide

  56. FEATURES
    Core features, not mere syntax
    56

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  63. TYPE CHECKING & TYPE EXPRESSION
    63

    View Slide

  64. TYPE CHECKING & TYPE EXPRESSION IN PYTHON
    64

    View Slide

  65. TYPE CHECKING & TYPE EXPRESSION
    65

    View Slide

  66. STRATEGY IN PYTHON
    Leveraging Python’s fundamental features
    66

    View Slide

  67. CHOOSE AN ALGORITHM AT RUN TIME
    67

    View Slide

  68. 68
    Context
    Strategy
    Concrete strategies
    CHOOSE AN ALGORITHM AT RUN TIME

    View Slide

  69. DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY
    69
    Instance of Strategy

    is given to Order
    constructor

    View Slide

  70. DOCTESTS FOR TWO ADDITIONAL CONCRETE STRATEGIES
    70

    View Slide

  71. VARIATIONS OF STRATEGY IN PYTHON
    Classic implementation using ABC


    First-class function implementation


    Parameterised closure implementation


    Parameterised callable implementation


    71

    View Slide

  72. CLASSIC STRATEGY: THE CONTEXT CLASS
    72
    Strategy is given to
    constructor
    Strategy is used here

    View Slide

  73. STRATEGY ABC AND A CONCRETE STRATEGY
    73

    View Slide

  74. TWO CONCRETE STRATEGIES
    74

    View Slide

  75. FIRST-CLASS FUNCTION


    STRATEGY
    75

    View Slide

  76. CONTEXT: STRATEGY FUNCTION AS ARGUMENT
    76
    Strategy function is
    passed to Order
    constructor

    View Slide

  77. CONTEXT: INVOKING THE STRATEGY FUNCTION
    77

    View Slide

  78. CONCRETE STRATEGIES AS FUNCTIONS
    78

    View Slide

  79. CONCRETE STRATEGIES AS FUNCTIONS (2)
    79

    View Slide

  80. VECTORIZING
    One aspect of functional programming
    80

    View Slide

  81. 81

    View Slide

  82. 82

    View Slide

  83. 83

    View Slide

  84. 84

    View Slide

  85. 85

    View Slide

  86. 86

    View Slide

  87. 87

    View Slide

  88. WRAPPING UP
    Learn the fundamentals
    88

    View Slide

  89. INSTEAD OF PARADIGMS…
    89

    View Slide

  90. FOCUS ON FUNDAMENTAL FEATURES
    90

    View Slide

  91. FEATURES ARE THE BEST GUIDE…
    91
    …to decide whether a particular pattern or implementation
    makes the most of the language.

    View Slide

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

    View Slide

  93. Luciano Ramalho


    [email protected]

    Fediverse: @[email protected]

    PFKAT: @ramalhoorg


    Repo: github.com/ramalho/beyond-paradigms


    Slides: speakerdeck.com/ramalho
    THANKS !

    View Slide