1 Beautiful Python

CodeWarrior

2019/10/11 发布于 技术 分类

文字内容
1. Elegance begets simplicity BEAUTIFUL PYTHON Great ideas of language design that make Python enjoyable and useful to so many people.
3. FLUENT PYTHON Published in 9 languages so far: • Chinese (traditional) • English • French • Book signing Japanese • at 12:00 Korean • today on Polish • 3rd floor Portuguese • Russian • Chinese (simplified) 2nd edition: Q3 of 2020 3
4. INTRODUCTION 4
5. To Love and create beauty are conditions to happiness. — Walter Gropius (1883–1969), founder of Bauhaus School
6. 6
7. 7
8. SIMPLE SYNTAX 8
9. A SIMPLE PROGRAM IN C #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; } 9
10. A SIMPLE PROGRAM IN C #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 10
11. A TRADITIONAL CHESS SET 11
12. A BAUHAUS CHESS SET BY JOSEF HARTWIG 12
13. A SIMPLE PROGRAM IN PYTHON import sys for arg in sys.argv: print arg 13
14. PYTHON: A MODERNIST LANGUAGE #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 14
15. PYTHON: A MODERNIST LANGUAGE 15
16. PYTHON: A MODERNIST LANGUAGE 16
17. PYTHON: A MODERNIST LANGUAGE 17
18. MODERNIST 18
19. 19
20. ANCIENT AND MODERNIST 20
21. 21
22. ENHANCED FOR 22
23. THAT SAME SIMPLE PROGRAM IN JAVA < 5 class Arguments { public static void main(String[] args) { for (int i=0; i < args.length; i++) System.out.println(args[i]); } } 23
24. Specialists are people who always repeat the same mistakes. — Walter Gropius
25. FOREACH: SINCE JAVA 5 The official name of the foreach syntax in Java is "enhanced for" class Arguments2 { public static void main(String[] args) { for (String arg : args) System.out.println(arg); } } 25
26. FOREACH: SINCE JAVA 5 The official name of the foreach syntax in Java is "enhanced for" class Arguments2 { public static void main(String[] args) { for (String arg : args) System.out.println(arg); } } import sys for arg in sys.argv: print arg 26
27. FOREACH: SINCE JAVA 5 The official name of the foreach syntax in Java is "enhanced for" year:'>year: 2004 class Arguments2 { public static void main(String[] args) { for (String arg : args) System.out.println(arg); } year:'>year: } 1991 import sys for arg in sys.argv: print arg 27
28. FOREACH IN BARBARA LISKOV'S CLU Barbara Liskov, Institute Professor at MIT 28
29. FOREACH IN BARBARA LISKOV'S CLU Prof. Liskov and her students Barbara Liskov, Institute Professor at MIT 29
30. CLU REFERENCE MANUAL year: 1975 CLU Reference Manual, p. 2 B. Liskov et. al. — © 1981 30 Springer-Verlag
31. ITERABLES 31
32. ITERABLE OBJECTS: THE KEY TO FOREACH CLU, Python, Java, etc. let we create iterable objects for item in an_iterable an_iterable: process(item) 32
33. ITERABLE OBJECTS: THE KEY TO FOREACH CLU, Python, Java, etc. let we create iterable objects for item in an_iterable an_iterable: process(item) 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) 33
34. avoidable belieavable extensible fixable iterable movable readable playable washable iterable, adj. — Capable of being iterated.
35. THE ITERATOR PATTERN The classic recipe 35
36. THE ITERATOR FROM THE GANG OF FOUR Design Patterns Gamma, Helm, Johnson & Vlissides ©1994 Addison-Wesley 36
37. Head First Design Patterns Poster O'Reilly ISBN 0-596-10214-3 37
38. 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) for item in an_iterable: process(item) •Terminates when a call to next() raises StopIteration. 38
39. AN ITERABLE TRAIN An instance of Train can be iterated, car by car >>> >>> ... car car car >>> t = Train(3) for car in t: print(car) #1 #2 #3 39
40. CLASSIC ITERATOR IMPLEMENTATION The pattern as described by Gamma et. al. 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 >>> >>> ... car car car car t = Train(4) for car in t: print(car) #1 #2 #3 #4 def __next__(self): if self.next <= self.last: self.next += 1 return 'car #%s' % (self.next) else: raise StopIteration() 40
41. COMPARE: CLASSIC ITERATOR × GENERATOR METHOD The classic Iterator recipe is obsolete in Python since v.2.2 (2001) class Train:'>Train: class Train:'>Train: def __init__(self, cars): self.cars = cars def __init__(self, cars): self.cars = cars def __iter__(self): return IteratorTrem(self.cars) def __iter__(self): for i in range(self.cars): yield 'car #%s' % (i+1) 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 41
42. 42
43. SPECIAL METHODS 43
44. THE PYTHONIC DEFINITION OF ITERABLE iterable, adj. — (Python) An object from which the iter() function can build an iterator. iter(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
45. SPECIAL METHODS 45
46. THE ZEN OF LEN 46
47. 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 userdefined __len__ special method. 47
48. 48
49. OPERATOR OVERLOADING 49
50. 50
51. EXAMPLE: SET OPERATORS 51
52. SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries Python set, frozenset: > 10 methods and operators Ruby Set:'>Set: > 10 methods and operators Elixir MapSet:'>Set: > 10 methods .Net (C# etc.) ISet interface:'>interface: > 10 methods; 2 implementations JavaScript (ES6) Set:'>Set: < 10 methods Java Set interface:'>interface: < 10 methods; 8 implementations Go Do it yourself, or pick one of several packages… 52
53. SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries Python set, frozenset: > 10 methods and operators 😻 Ruby Set:'>Set: > 10 methods and operators 😻 Elixir MapSet:'>Set: > 10 methods 😻 .Net (C# etc.) ISet interface:'>interface: > 10 methods; 2 implementations 😻 JavaScript (ES6) Set:'>Set: < 10 methods Java Set interface:'>interface: < 10 methods; 8 implementations Go Do it yourself, or pick one of several packages… 53
54. SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries Python set, frozenset: > 10 methods and operators 😻 Ruby Set:'>Set: > 10 methods and operators 😻 Elixir MapSet:'>Set: > 10 methods 😻 .Net (C# etc.) ISet interface:'>interface: > 10 methods; 2 implementations 😻 JavaScript (ES6) Set:'>Set: < 10 methods 😿 Java Set interface:'>interface: < 10 methods; 8 implementations 😿 Go Do it yourself, or pick one of several packages… 54
55. SETS IN A FEW LANGUAGES AND PLATFORMS Some languages/platform APIs that implement sets in their standard libraries Python set, frozenset: > 10 methods and operators 😻 Ruby Set:'>Set: > 10 methods and operators 😻 Elixir MapSet:'>Set: > 10 methods 😻 .Net (C# etc.) ISet interface:'>interface: > 10 methods; 2 implementations 😻 JavaScript (ES6) Set:'>Set: < 10 methods 😿 Java Set interface:'>interface: < 10 methods; 8 implementations 😿 Go Do it yourself, or pick one of several packages… 😾 55
56. FULL SET OPERATORS IN PYTHON In te rse ct ion Un ion Sy mmet ric dif fe re nc e (a .k. a. XOR) Dif fe re nc e 56
57. USING INFIX OPERATORS: DE MORGAN’S LAW 57
58. SET OPERATORS AND METHODS (1) 58
59. SET OPERATORS AND METHODS (2) Differences: 59
60. SET TESTS All of these return a bool: 60
61. CODE: UINTSET CLASS 61
62. 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: https://github.com/standupdev/uintset 62
63. 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
64. 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
65. 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
66. REPRESENTING SETS OF INTEGERS AS BIT PATTERNS This set: UintSet({290}) Is represented by this integer 1989292945639146568621528992587283360401824603189390869761855907572637988050133 502132224 Which has this bit pattern: 1000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000 66
67. 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 UintSet({10}) → 1024 │1│1│1│1│1│1│1│1│1│1│ └─┴─┴─┴─┴─┴─┴─┴─┴─┴─┘ │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
68. SAMPLE METHOD: INTERSECTION OPERATOR & https://github.com/standupdev/uintset 68
70. THE BEAUTY OF DOUBLE DISPATCH 70
71. FAIL FAST 71
72. 72
73. 73
74. NO "UNDEFINED" VALUES 74
75. NO SURPRISING NULL VALUES FROM DICTS 75
76. 76
77. NO SURPRISING NULL VALUES FROM DICTS 77
78. NO SURPRISING NULL VALUES WHEN UNPACKING 78
79. NO SURPRISING NULL VALUES WHEN UNPACKING 79
80. 80
81. NO SURPRISING RESULTS FOR "2" + 8 81
82. THE LAST PARAGRAPH OF FLUENT PYTHON 82
83. COMMUNITY 83
84. 84
85. PYTHON BRASIL 2018 BY JULIO MELANDA (INSTAGRAM @JULIOMELANDA) 85
86. 86
87. 87
88. 88
89. 89
90. 90
91. 91
92. 92
93. 93
94. ELEGANCE 94
95. FIRST QUOTATION IN CHAPTER 1 OF FLUENT PYTHON 95
96. 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
97. 97
99. 易易經 Yìjīng 99
100. 100
104. Elegance Begets Simplicity
107. import sys for arg in sys.argv: print arg
108. 谢谢 e-mail: luciano.ramalho@thoughtworks.com Twitter: @ramalhoorg WeChat: Luciano_Ramalho