Slide 1

Slide 1 text

BEAUTIFUL PYTHON Great ideas of language design that make Python enjoyable and useful to so many people. E l e g a n c e b e g e t s s i m p l i c i t y

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

FLUENT PYTHON Published in 9 languages so far: •Chinese (simplified) •Chinese (traditional) •English •French •Japanese •Korean •Polish •Portuguese •Russian
 2nd edition: Q3 of 2020 3 Book signing at 12:00 today on 3rd floor

Slide 4

Slide 4 text

INTRODUCTION 4

Slide 5

Slide 5 text

To Love and create beauty are conditions to happiness. — Walter Gropius (1883–1969), founder of Bauhaus School

Slide 6

Slide 6 text

6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

SIMPLE SYNTAX 8

Slide 9

Slide 9 text

A SIMPLE PROGRAM IN C 9 #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }

Slide 10

Slide 10 text

A SIMPLE PROGRAM IN C 10 #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; } $ ./args alpha bravo charlie ./args alpha bravo charlie

Slide 11

Slide 11 text

A TRADITIONAL CHESS SET 11

Slide 12

Slide 12 text

A BAUHAUS CHESS SET BY JOSEF HARTWIG 12

Slide 13

Slide 13 text

A SIMPLE PROGRAM IN PYTHON 13 import sys for arg in sys.argv: print arg

Slide 14

Slide 14 text

PYTHON: A MODERNIST LANGUAGE 14 #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; } import sys for arg in sys.argv: print arg

Slide 15

Slide 15 text

PYTHON: A MODERNIST LANGUAGE 15

Slide 16

Slide 16 text

PYTHON: A MODERNIST LANGUAGE 16

Slide 17

Slide 17 text

PYTHON: A MODERNIST LANGUAGE 17

Slide 18

Slide 18 text

MODERNIST 18

Slide 19

Slide 19 text

19

Slide 20

Slide 20 text

ANCIENT AND MODERNIST 20

Slide 21

Slide 21 text

21

Slide 22

Slide 22 text

ENHANCED FOR 22

Slide 23

Slide 23 text

THAT SAME SIMPLE PROGRAM IN JAVA < 5 23 class Arguments { public static void main(String[] args) { for (int i=0; i < args.length; i++) System.out.println(args[i]); } }

Slide 24

Slide 24 text

Specialists are people who always repeat the same mistakes. 
 — Walter Gropius

Slide 25

Slide 25 text

class Arguments2 { public static void main(String[] args) { for (String arg : args) System.out.println(arg); } } FOREACH: SINCE JAVA 5 25 The official name of the foreach syntax in Java is "enhanced for"

Slide 26

Slide 26 text

class Arguments2 { public static void main(String[] args) { for (String arg : args) System.out.println(arg); } } FOREACH: SINCE JAVA 5 26 The official name of the foreach syntax in Java is "enhanced for" import sys for arg in sys.argv: print arg

Slide 27

Slide 27 text

class Arguments2 { public static void main(String[] args) { for (String arg : args) System.out.println(arg); } } FOREACH: SINCE JAVA 5 27 The official name of the foreach syntax in Java is "enhanced for" year: 2004 import sys for arg in sys.argv: print arg year: 1991

Slide 28

Slide 28 text

FOREACH IN BARBARA LISKOV'S CLU 28 Barbara Liskov, Institute Professor at MIT

Slide 29

Slide 29 text

FOREACH IN BARBARA LISKOV'S CLU 29 Barbara Liskov, Institute Professor at MIT Prof. Liskov and her students

Slide 30

Slide 30 text

CLU REFERENCE MANUAL 30 year: 1975 CLU Reference Manual, p. 2 B. Liskov et. al. — © 1981 Springer-Verlag

Slide 31

Slide 31 text

ITERABLES 31

Slide 32

Slide 32 text

CLU, Python, Java, etc. let we create iterable objects
 
 
 
 ITERABLE OBJECTS: THE KEY TO FOREACH 32 for item in an_iterable: process(item) an_iterable

Slide 33

Slide 33 text

CLU, Python, Java, etc. let we create iterable objects
 
 
 
 
 
 
 Some languages don't offer this flexibility 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 syntax) ITERABLE OBJECTS: THE KEY TO FOREACH 33 for item in an_iterable: process(item) an_iterable

Slide 34

Slide 34 text

avoidable belieavable extensible fixable iterable movable readable playable washable iterable, adj. — Capable of being iterated.

Slide 35

Slide 35 text

THE ITERATOR PATTERN The classic recipe 35

Slide 36

Slide 36 text

THE ITERATOR FROM THE GANG OF FOUR Design Patterns
 Gamma, Helm, Johnson & Vlissides
 ©1994 Addison-Wesley 36

Slide 37

Slide 37 text

37 Head First Design Patterns Poster
 O'Reilly
 ISBN 0-596-10214-3

Slide 38

Slide 38 text

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) 38 for item in an_iterable: process(item) for item in an_iterable: process(item) •Terminates when a call to next() raises StopIteration.

Slide 39

Slide 39 text

AN ITERABLE TRAIN An instance of Train can be iterated, car by car 39 >>> t = Train(3) >>> for car in t: ... print(car) car #1 car #2 car #3 >>>

Slide 40

Slide 40 text

CLASSIC ITERATOR IMPLEMENTATION The pattern as described by Gamma et. al. 40 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

Slide 41

Slide 41 text

COMPARE: CLASSIC ITERATOR × GENERATOR METHOD The classic Iterator recipe is obsolete in Python since v.2.2 (2001) 41 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 keeps the state of the iteration in its own local scope

Slide 42

Slide 42 text

42

Slide 43

Slide 43 text

SPECIAL METHODS 43

Slide 44

Slide 44 text

THE PYTHONIC DEFINITION OF ITERABLE Returns iterator for iterable by: • invoking __iter__ (if available)
 or • building an iterator to fetch items via __getitem__ with 
 0-based indices (seq[0], seq[1], etc…) 44 iterable, adj. — (Python) An object from which the iter() function can build an iterator. iter(iterable):

Slide 45

Slide 45 text

SPECIAL METHODS 45

Slide 46

Slide 46 text

THE ZEN OF LEN 46

Slide 47

Slide 47 text

LEN FUNCTION AND __LEN__ METHOD 
 Why do we call len(x) and not x.len()? For performance, the length of a sequence must be fast to get. For built-in collections (str, list, tuple, array, set, etc.) and extensions (Numpy arrays), the interpreter uses the C API to get the length from the ob_size field in the PyVarObject struct in memory. For collections defined in Python code, the interpreter calls the user- defined __len__ special method. 47

Slide 48

Slide 48 text

48

Slide 49

Slide 49 text

OPERATOR OVERLOADING 49

Slide 50

Slide 50 text

50

Slide 51

Slide 51 text

EXAMPLE: SET OPERATORS 51

Slide 52

Slide 52 text

SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries 52 Python set, frozenset: > 10 methods and operators Ruby Set: > 10 methods and operators Elixir MapSet: > 10 methods .Net (C# etc.) ISet interface: > 10 methods; 2 implementations JavaScript (ES6) Set: < 10 methods Java Set interface: < 10 methods; 8 implementations Go Do it yourself, or pick one of several packages…

Slide 53

Slide 53 text

SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries 53 Python set, frozenset: > 10 methods and operators Ruby Set: > 10 methods and operators Elixir MapSet: > 10 methods .Net (C# etc.) ISet interface: > 10 methods; 2 implementations JavaScript (ES6) Set: < 10 methods Java Set interface: < 10 methods; 8 implementations Go Do it yourself, or pick one of several packages…

Slide 54

Slide 54 text

SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries 54 Python set, frozenset: > 10 methods and operators Ruby Set: > 10 methods and operators Elixir MapSet: > 10 methods .Net (C# etc.) ISet interface: > 10 methods; 2 implementations JavaScript (ES6) Set: < 10 methods Java Set interface: < 10 methods; 8 implementations Go Do it yourself, or pick one of several packages…

Slide 55

Slide 55 text

SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries 55 Python set, frozenset: > 10 methods and operators Ruby Set: > 10 methods and operators Elixir MapSet: > 10 methods .Net (C# etc.) ISet interface: > 10 methods; 2 implementations JavaScript (ES6) Set: < 10 methods Java Set interface: < 10 methods; 8 implementations Go Do it yourself, or pick one of several packages…

Slide 56

Slide 56 text

FULL SET OPERATORS IN PYTHON 56 Intersection Union Symmetric difference (a.k.a. XOR) Difference

Slide 57

Slide 57 text

USING INFIX OPERATORS: DE MORGAN’S LAW 57

Slide 58

Slide 58 text

SET OPERATORS AND METHODS (1) 58

Slide 59

Slide 59 text

SET OPERATORS AND METHODS (2) Differences: 59

Slide 60

Slide 60 text

SET TESTS All of these return a bool: 60

Slide 61

Slide 61 text

CODE: UINTSET CLASS 61

Slide 62

Slide 62 text

UINTSET: A SET CLASS FOR NON-NEGATIVE INTEGERS Inspired by the intset example in chapter 6 of The Go Programming Language by A. Donovan and B. Kernighan An empty set is represented by zero.
 A set of integers {a, b, c} is represented by on bits in an integer at offsets a, b, and c. Source code: 62 https://github.com/standupdev/uintset

Slide 63

Slide 63 text

REPRESENTING SETS OF INTEGERS AS BIT PATTERNS This set: UintSet({13, 14, 22, 28, 38, 53, 64, 76, 94, 102, 107, 121, 136, 143, 150, 157, 169, 173, 187, 201, 213, 216, 234, 247, 257, 268, 283, 288, 290}) 63

Slide 64

Slide 64 text

REPRESENTING SETS OF INTEGERS AS BIT PATTERNS This set: UintSet({13, 14, 22, 28, 38, 53, 64, 76, 94, 102, 107, 121, 136, 143, 150, 157, 169, 173, 187, 201, 213, 216, 234, 247, 257, 268, 283, 288, 290}) Is represented by this integer 2502158007702946921897431281681230116680925854234644385938703363396454971897652 283727872 64

Slide 65

Slide 65 text

REPRESENTING SETS OF INTEGERS AS BIT PATTERNS This set: UintSet({13, 14, 22, 28, 38, 53, 64, 76, 94, 102, 107, 121, 136, 143, 150, 157, 169, 173, 187, 201, 213, 216, 234, 247, 257, 268, 283, 288, 290}) Is represented by this integer 2502158007702946921897431281681230116680925854234644385938703363396454971897652 283727872
 Which has this bit pattern: 1010000100000000000000100000000001000000000100000000000010000000000000000010010 0000000000100000000000001000000000000010001000000000001000000100000010000001000 0000000000010000000000000100001000000010000000000000000010000000000010000000000 100000000000000100000000010000010000000110000000000000 65

Slide 66

Slide 66 text

REPRESENTING SETS OF INTEGERS AS BIT PATTERNS This set: UintSet({290}) 66 Is represented by this integer 1989292945639146568621528992587283360401824603189390869761855907572637988050133 502132224
 Which has this bit pattern: 1000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000

Slide 67

Slide 67 text

REPRESENTING SETS OF INTEGERS AS BIT PATTERNS (2) UintSet() → 0 │0│ └─┘ UintSet({0}) → 1 │1│ └─┘ UintSet({1}) → 2 │1│0│ └─┴─┘ UintSet({0, 1, 2, 4, 8}) → 279 │1│0│0│0│1│0│1│1│1│ └─┴─┴─┴─┴─┴─┴─┴─┴─┘ UintSet({0, 1, 2, 3, 4, 5, 6, 7, 8, 9}) → 1023 │1│1│1│1│1│1│1│1│1│1│ └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ UintSet({10}) → 1024 │1│0│0│0│0│0│0│0│0│0│0│ └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ UintSet({0, 2, 4, 6, 8, 10, 12, 14, 16, 18}) → 349525
 │1│0│1│0│1│0│1│0│1│0│1│0│1│0│1│0│1│0│1│ └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ UintSet({1, 3, 5, 7, 9, 11, 13, 15, 17, 19}) → 699050
 
 │1│0│1│0│1│0│1│0│1│0│1│0│1│0│1│0│1│0│1│0│ └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ 67

Slide 68

Slide 68 text

SAMPLE METHOD: INTERSECTION OPERATOR & 68 https://github.com/standupdev/uintset

Slide 69

Slide 69 text

69 https://github.com/standupdev/uintset

Slide 70

Slide 70 text

THE BEAUTY OF DOUBLE DISPATCH 70

Slide 71

Slide 71 text

FAIL FAST 71

Slide 72

Slide 72 text

72

Slide 73

Slide 73 text

73

Slide 74

Slide 74 text

NO "UNDEFINED" VALUES 74

Slide 75

Slide 75 text

NO SURPRISING NULL VALUES FROM DICTS 75

Slide 76

Slide 76 text

76

Slide 77

Slide 77 text

NO SURPRISING NULL VALUES FROM DICTS 77

Slide 78

Slide 78 text

NO SURPRISING NULL VALUES WHEN UNPACKING 78

Slide 79

Slide 79 text

NO SURPRISING NULL VALUES WHEN UNPACKING 79

Slide 80

Slide 80 text

80

Slide 81

Slide 81 text

NO SURPRISING RESULTS FOR "2" + 8 81

Slide 82

Slide 82 text

THE LAST PARAGRAPH OF FLUENT PYTHON 82

Slide 83

Slide 83 text

COMMUNITY 83

Slide 84

Slide 84 text

84

Slide 85

Slide 85 text

PYTHON BRASIL 2018 BY JULIO MELANDA (INSTAGRAM @JULIOMELANDA) 85

Slide 86

Slide 86 text

86

Slide 87

Slide 87 text

87

Slide 88

Slide 88 text

88

Slide 89

Slide 89 text

89

Slide 90

Slide 90 text

90

Slide 91

Slide 91 text

91

Slide 92

Slide 92 text

92

Slide 93

Slide 93 text

93

Slide 94

Slide 94 text

ELEGANCE 94

Slide 95

Slide 95 text

FIRST QUOTATION IN CHAPTER 1 OF FLUENT PYTHON 95

Slide 96

Slide 96 text

BRUCE ECKEL Author of: • On Java 8 • Thinking in Java • Atomic Scala • Atomic Kotlin • and many other books. Loves Python . Designed t-shirt for PyCon US 2009. 96

Slide 97

Slide 97 text

97

Slide 98

Slide 98 text

No content

Slide 99

Slide 99 text

99 易易經 Yìjīng

Slide 100

Slide 100 text

100

Slide 101

Slide 101 text

No content

Slide 102

Slide 102 text

No content

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

Elegance Begets Simplicity

Slide 105

Slide 105 text

No content

Slide 106

Slide 106 text

No content

Slide 107

Slide 107 text

import sys for arg in sys.argv: print arg

Slide 108

Slide 108 text

e-mail: [email protected] Twitter: @ramalhoorg WeChat: Luciano_Ramalho 谢谢

Slide 109

Slide 109 text

No content