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

Beautiful Python

Beautiful Python

Great ideas of language design that make Python enjoyable and useful to so many people. PyCon China 2019 keynote.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

September 21, 2019
Tweet

Transcript

  1. 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
  2. None
  3. 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
  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 9 #include <stdio.h> int main(int

    argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }
  10. A SIMPLE PROGRAM IN C 10 #include <stdio.h> 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
  11. A TRADITIONAL CHESS SET 11

  12. A BAUHAUS CHESS SET BY JOSEF HARTWIG 12

  13. A SIMPLE PROGRAM IN PYTHON 13 import sys for arg

    in sys.argv: print arg
  14. PYTHON: A MODERNIST LANGUAGE 14 #include <stdio.h> 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
  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 23 class

    Arguments { public static void main(String[] args) { for (int i=0; i < args.length; i++) System.out.println(args[i]); } }
  24. Specialists are people who always repeat the same mistakes. 


    — Walter Gropius
  25. 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"
  26. 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
  27. 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
  28. FOREACH IN BARBARA LISKOV'S CLU 28 Barbara Liskov, Institute Professor

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

    at MIT Prof. Liskov and her students
  30. CLU REFERENCE MANUAL 30 year: 1975 CLU Reference Manual, p.

    2 B. Liskov et. al. — © 1981 Springer-Verlag
  31. ITERABLES 31

  32. 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
  33. 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
  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. 37 Head First Design Patterns Poster
 O'Reilly
 ISBN 0-596-10214-3

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

  43. SPECIAL METHODS 43

  44. 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):
  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 user- defined __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 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…
  53. 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…
  54. 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…
  55. 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…
  56. FULL SET OPERATORS IN PYTHON 56 Intersection Union Symmetric difference

    (a.k.a. XOR) Difference
  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: 62 https://github.com/standupdev/uintset
  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})

    66 Is represented by this integer 1989292945639146568621528992587283360401824603189390869761855907572637988050133 502132224
 Which has this bit pattern: 1000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 0000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000
  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 │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
  68. SAMPLE METHOD: INTERSECTION OPERATOR & 68 https://github.com/standupdev/uintset

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

  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

  98. None
  99. 99 易易經 Yìjīng

  100. 100

  101. None
  102. None
  103. None
  104. Elegance Begets Simplicity

  105. None
  106. None
  107. import sys for arg in sys.argv: print arg

  108. e-mail: luciano.ramalho@thoughtworks.com Twitter: @ramalhoorg WeChat: Luciano_Ramalho 谢谢

  109. None