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
@ramalhoorg
Slide 2
Slide 2 text
2
Sometimes you need a blank template.
Slide 3
Slide 3 text
FLUENT PYTHON, MY FIRST BOOK
Fluent Python (O’Reilly, 2015)
Python Fluente (Novatec, 2015)
Python к вершинам
мастерства (DMK, 2015)
流暢的 Python (Gotop, 2016)
also in Polish, Korean, etc…
3
4.7 stars at
Amazon.com
Slide 4
Slide 4 text
PARADIGMS
Programming language categories
4
Slide 5
Slide 5 text
CALCULATOR PROGRAMMING IS IMPERATIVE
5
HP-25 TI 58C
Slide 6
Slide 6 text
HP-25 CALCULATOR PROGRAMMING LANGUAGE
6
Slide 7
Slide 7 text
A SURVEY-STYLE PROGRAMMING LANGUAGES BOOK
Programming
Language
Pragmatics,
4th edition (2015)
Michael L. Scott
7
Slide 8
Slide 8 text
GCD ASM X86
Greatest
common divisor
in x86 Assembly
(Scott, 2015)
8
Slide 9
Slide 9 text
GCD IN C, OCAML AND PROLOG
9
Slide 10
Slide 10 text
GCD IN PYTHON
Imperative style Functional style
10
Slide 11
Slide 11 text
GCD IN PYTHON
Imperative style Functional style
11
Bad fit for Python:
no tail-call
optimisation
Slide 12
Slide 12 text
ONE CLASSIFICATION
12
Programming
Language
Pragmatics,
4th edition (2015)
Michael L. Scott
Slide 13
Slide 13 text
ONE CLASSIFICATION (ZOOM)
13
Slide 14
Slide 14 text
ONE CLASSIFICATION (ZOOM)
14
???
Slide 15
Slide 15 text
ANOTHER BOOK
Princípios de Linguagens de
Programação
(2003)
Ana Cristina Vieira de Melo
Flávio Soares Corrêa da Silva
15
Slide 16
Slide 16 text
16
Slide 17
Slide 17 text
17
Lógicas
Slide 18
Slide 18 text
THE LANGUAGE LIST
18
Slide 19
Slide 19 text
LANGUAGES ARE MISSING…
19
Slide 20
Slide 20 text
LANGUAGE CATEGORIES
20
Slide 21
Slide 21 text
LANGUAGE CATEGORIES (2)
21
Slide 22
Slide 22 text
LANGUAGE CATEGORIES (3)
22
Slide 23
Slide 23 text
LANGUAGE CATEGORIES (4)
23
Slide 24
Slide 24 text
CATEGORIES?
Ontologies are so 1900’s
24
Slide 25
Slide 25 text
A CLASSIFICATION BASED ON HARD FACTS
25
Slide 26
Slide 26 text
A CLASSIFICATION BASED ON HARD FACTS?
26
“Noble” gases!?
Slide 27
Slide 27 text
“Ontology is overrated.”
Clay Shirky
27
Slide 28
Slide 28 text
A BETTER APPROACH
Fundamental Features of Programming Languages
28
Slide 29
Slide 29 text
TEACHING PROGRAMMING LANGUAGE THEORY
29
Slide 30
Slide 30 text
A PAPER PRESENTING THE APPROACH
30
Slide 31
Slide 31 text
A PAPER PRESENTING THE APPROACH
31
Slide 32
Slide 32 text
A PAPER PRESENTING THE APPROACH
32
Slide 33
Slide 33 text
A PAPER PRESENTING THE APPROACH
33
Slide 34
Slide 34 text
THEORY IN PRACTICE WITH RACKET (A SCHEME DIALECT)
34
Slide 35
Slide 35 text
THEORY IN PRACTICE WITH PASCAL (1990)
35
Slide 36
Slide 36 text
DESIGN PATTERNS
When languages fall short
36
Slide 37
Slide 37 text
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
37
Slide 38
Slide 38 text
FAULTS IN THE SPREAD OF PATTERNS
38
Slide 39
Slide 39 text
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.
39
Slide 40
Slide 40 text
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.
40
Slide 41
Slide 41 text
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.
41
Slide 42
Slide 42 text
42
Slide 43
Slide 43 text
43
Slide 44
Slide 44 text
44
Slide 45
Slide 45 text
45
Slide 46
Slide 46 text
FEATURES
Core features, not mere syntax
46
Slide 47
Slide 47 text
SAMPLE FEATURES ✖ LANGUAGES
47
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
Slide 48
Slide 48 text
SAMPLE FEATURES ✖ LANGUAGES
48
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
Slide 49
Slide 49 text
SAMPLE FEATURES ✖ LANGUAGES
49
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
Slide 50
Slide 50 text
SAMPLE FEATURES ✖ LANGUAGES
50
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
Slide 51
Slide 51 text
SAMPLE FEATURES ✖ LANGUAGES
51
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
Slide 52
Slide 52 text
SAMPLE FEATURES ✖ LANGUAGES
52
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
Slide 53
Slide 53 text
STRATEGY IN PYTHON
Leveraging Python’s fundamental features
53
Slide 54
Slide 54 text
CHOOSE AN ALGORITHM AT RUN TIME
54
Slide 55
Slide 55 text
55
Context Strategy
Concrete strategies
CHOOSE AN ALGORITHM AT RUN TIME
Slide 56
Slide 56 text
DOCTESTS FOR CONTEXT AND ONE CONCRETE STRATEGY
56
Instance of Strategy
is given to Order
constructor
Slide 57
Slide 57 text
DOCTESTS FOR TWO ADDITIONAL CONCRETE STRATEGIES
57
Slide 58
Slide 58 text
VARIATIONS OF STRATEGY IN PYTHON
Classic implementation using ABC
First-class function implementation
Parameterised closure implementation
Parameterised callable implementation
58
Slide 59
Slide 59 text
CLASSIC STRATEGY: THE CONTEXT CLASS
59
Strategy is given to
constructor
Strategy is used here
Slide 60
Slide 60 text
STRATEGY ABC AND A CONCRETE STRATEGY
60
Slide 61
Slide 61 text
TWO CONCRETE STRATEGIES
61
Slide 62
Slide 62 text
FIRST-CLASS FUNCTION
STRATEGY
62
Slide 63
Slide 63 text
CONTEXT: STRATEGY FUNCTION AS ARGUMENT
63
Strategy function is
passed to Order
constructor
Slide 64
Slide 64 text
CONTEXT: INVOKING THE STRATEGY FUNCTION
64
Slide 65
Slide 65 text
CONCRETE STRATEGIES AS FUNCTIONS
65
Slide 66
Slide 66 text
CONCRETE STRATEGIES AS FUNCTIONS (2)
66
Slide 67
Slide 67 text
PARAMETERISED
STRATEGY
WITH CLOSURE
67
Slide 68
Slide 68 text
PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE
68
Promo is called with
discount percent value
Slide 69
Slide 69 text
PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE
69
Outer function gets
percent argument
Inner function carries
percent binding
in its closure
Slide 70
Slide 70 text
LAMBDA: SHORTCUT TO DECLARE INNER FUNCTION
70
Slide 71
Slide 71 text
PARAMETERISED
STRATEGY
WITH CALLABLE
71
Slide 72
Slide 72 text
PARAMETERISED STRATEGY IMPLEMENTED AS CLOSURE
72
Promo is instantiated
with discount percent
value
Slide 73
Slide 73 text
PROMOTION AS A CALLABLE CLASS
73
Slide 74
Slide 74 text
CONCRETE STRATEGIES AS CALLABLE CONCRETE CLASSES
74
Slide 75
Slide 75 text
WHICH IS MORE
IDIOMATIC?
Classes x functions
75
Slide 76
Slide 76 text
WHICH IS MORE IDIOMATIC?
Classic strategy feels too verbose for Python*
First-class functions are very common in the standard library
•The sorted built-in key argument is one example.
76
* Yes, this is subjective. I am talking about style!
Slide 77
Slide 77 text
WHICH IS MORE IDIOMATIC — WITH PARAMETER?
Use of closure is common in Python
•nonlocal was added in Python 3 to support it better
Callable objects are uniquely Pythonic
•Graham Dumpleton recommends callable classes as the best way to code decorators
77
Slide 78
Slide 78 text
WRAPPING UP
Learn the fundamentals
78
Slide 79
Slide 79 text
WHY LEARN THE FUNDAMENTALS*
Learn new languages faster
Leverage language features
Choose among alternative implementations
Make good use of design patterns
Debug hard problems
Emulate missing features when they are missing
79
Inspired by Programming
Language Pragmatics
Michael L. Scott
*