Dustin Ingram
August 08, 2018
10k

# PEP 572: The Walrus Operator

A discussion on Python Governance, an introduction to PEP 572 and the walrus operator, as well as the resulting fallout.

August 08, 2018

## Transcript

1. AUSTIN PYTHON MEETUP
AUGUST 2018

2. HI, I'M DUSTIN

3. PEP 572

4. PYTHON GOVERNANCE

5. BDFL
BENEVOLENT DICTATOR
FOR LIFE

6. PEP
PYTHON ENHANCEMENT PROPOSAL

7. PEP 8
STYLE GUIDE FOR PYTHON CODE

8. PEP 8
STYLE GUIDE FOR PYTHON CODE
AUTHOR: GUIDO VAN ROSSUM

9. PEP 20
THE ZEN OF PYTHON

10. PEP 20
THE ZEN OF PYTHON
AUTHOR: TIM PETERS

11. PEP 566
PACKAGES 2.1

12. PEP 566
PACKAGES 2.1
AUTHOR: DUSTIN INGRAM

13. BDFL
DELEGATES

14. 1) DRAFT
2) ACCEPTANCE
3) IMPLEMENTATION

15. PEP 572

16. :=

17. PROBLEMS

18. match = pattern.search(data)
if match is not None:
do_something(match)

while chunk:
process(chunk)

20. foo = [f(x), f(x)**2, f(x)**3]

21. y = f(x)
foo = [y, y**2, y**3]

22. results = []
for x in data:
result = f(x)
if result is not None:
results.append(result)

23. filtered_data = [
f(x) for x in data
if f(x) is not None
]

24. SOLUTIONS

25. match = pattern.search(data)
if match is not None:
do_something(match)

26. if (match := pattern.search(data)) is not None:
do_something(match)

while chunk:
process(chunk)

process(chunk)

29. foo = [f(x), f(x)**2, f(x)**3]

30. foo = [y := f(x), y**2, y**3]

31. results = []
for x in data:
result = f(x)
if result is not None:
results.append(result)

32. filtered_data = [
f(x) for x in data
if f(x) is not None
]

33. filtered_data = [
y for x in data
if (y := f(x)) is not None
]

34. WHY?

35. LESS LINES
ARE BETTER

36. foo = [y := f(x), y**2, y**3]

37. y = f(x)
foo = [y, y**2, y**3]

38. LESS LINES
ARE MORE EFFICIENT?

39. match = re.match(data)
group = match.group(1) if match else None

40. group = re.match(data).group(1) if re.match(data) else None

41. group = match.group(1) if (match := re.match(data)) else None

42. SCOPE

43. foo = 'foo'
if (foo := 'bar') is not None:
do_something(...)
print(foo)

44. EXCEPTIONS!

45. if any((comment := line).startswith('#') for line in lines):
print("First comment:", comment)
else:

46. if all((nonblank := line).strip() == '' for line in lines):
print("All lines are blank")
else:
print("First non-blank line:", nonblank)

47. total = 0
partial_sums = [total := total + v for v in values]
print("Total:", total)

48. =

49. x = y = z = 0 # Yes
(z := (y := (x := 0))) # No

50. a[i] = x # Yes
a[i] := x # No
self.rest = [] # Yes
self.rest := [] # No

51. x = 1, 2 # Sets x to (1, 2)
(x := 1, 2) # Sets x to 1

52. total += tax # Yes
total +:= tax # No

53. = VS. ==

54. PEP 572

55. RECEPTION

56. BACKWARDS
COMPATIBILITY
"WHERE WILL THIS WORK?"

57. TEACHABILITY
"WHAT DO WE CALL THIS?"

58. ATTRACTIVENESS
"IT'S JUST UGLY!"

59. TIM PETERS:
"THE CURRENT PROPOSAL WOULD
HAVE ALLOWED A MODEST BUT
CLEAR IMPROVEMENT IN QUITE A
FEW BITS OF CODE."

60. BARRY WARSAW:
"SINCE IT CHANGES THE SYNTAX OF
THE LANGUAGE, PEOPLE TEND TO
FOCUS ON THAT WITHOUT
UNDERSTANDING THE DEEPER
SEMANTIC ISSUES."

61. ŁUKASZ LANGA:
"DICTATORS SHOULD DICTATE"

62. GUIDO VAN ROSSUM:
SO HE WOULDN'T "GO INSANE"

63. QUESTIONS

64. WHAT DOES THIS
MEAN FOR PYTHON?

65. IT'S GOING
TO BE OK

66. WILL THIS BECOME
PART OF PYTHON?

67. PROBABLY
(IN 3.8)

68. BUT I DON'T LIKE IT!

69. THEN DON'T
WRITE IT!

70. IS GUIDO
COMING BACK?

71. PROBABLY NOT

72. WILL THIS
HAPPEN AGAIN?

73. PROBABLY NOT

74. ¯\_(ϑ)_/¯

75. THANKS!