PEP 572: The Walrus Operator

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.

8db101b3a812b0464c53e5409c12ea94?s=128

Dustin Ingram

August 08, 2018
Tweet

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. None
  7. PEP PYTHON ENHANCEMENT PROPOSAL

  8. PEP 8 STYLE GUIDE FOR PYTHON CODE

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

    ROSSUM
  10. PEP 20 THE ZEN OF PYTHON

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

  12. PEP 566 METADATA FOR PYTHON SOFTWARE PACKAGES 2.1

  13. PEP 566 METADATA FOR PYTHON SOFTWARE PACKAGES 2.1 AUTHOR: DUSTIN

    INGRAM
  14. BDFL DELEGATES

  15. 1) DRAFT 2) ACCEPTANCE 3) IMPLEMENTATION

  16. PEP 572

  17. :=

  18. None
  19. None
  20. PROBLEMS

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

  22. chunk = file.read(8192) while chunk: process(chunk) chunk = file.read(8192)

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

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

  25. results = [] for x in data: result = f(x)

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

    is not None ]
  27. SOLUTIONS

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

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

  30. chunk = file.read(8192) while chunk: process(chunk) chunk = file.read(8192)

  31. while chunk := file.read(8192): process(chunk)

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

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

  34. results = [] for x in data: result = f(x)

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

    is not None ]
  36. filtered_data = [ y for x in data if (y

    := f(x)) is not None ]
  37. WHY?

  38. LESS LINES ARE BETTER

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

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

  41. LESS LINES ARE MORE EFFICIENT?

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

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

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

  45. SCOPE

  46. foo = 'foo' if (foo := 'bar') is not None:

    do_something(...) print(foo)
  47. EXCEPTIONS!

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

    comment) else: print("There are no comments")
  49. if all((nonblank := line).strip() == '' for line in lines):

    print("All lines are blank") else: print("First non-blank line:", nonblank)
  50. total = 0 partial_sums = [total := total + v

    for v in values] print("Total:", total)
  51. =

  52. x = y = z = 0 # Yes (z

    := (y := (x := 0))) # No
  53. a[i] = x # Yes a[i] := x # No

    self.rest = [] # Yes self.rest := [] # No
  54. x = 1, 2 # Sets x to (1, 2)

    (x := 1, 2) # Sets x to 1
  55. total += tax # Yes total +:= tax # No

  56. = VS. ==

  57. PEP 572

  58. RECEPTION

  59. BACKWARDS COMPATIBILITY "WHERE WILL THIS WORK?"

  60. TEACHABILITY "WHAT DO WE CALL THIS?"

  61. ATTRACTIVENESS "IT'S JUST UGLY!"

  62. None
  63. None
  64. None
  65. None
  66. TIM PETERS: "THE CURRENT PROPOSAL WOULD HAVE ALLOWED A MODEST

    BUT CLEAR IMPROVEMENT IN QUITE A FEW BITS OF CODE."
  67. BARRY WARSAW: "SINCE IT CHANGES THE SYNTAX OF THE LANGUAGE,

    PEOPLE TEND TO FOCUS ON THAT WITHOUT UNDERSTANDING THE DEEPER SEMANTIC ISSUES."
  68. ŁUKASZ LANGA: "DICTATORS SHOULD DICTATE"

  69. GUIDO VAN ROSSUM: HAD TO STOP READING THE THREADS SO

    HE WOULDN'T "GO INSANE"
  70. None
  71. None
  72. QUESTIONS

  73. WHAT DOES THIS MEAN FOR PYTHON?

  74. IT'S GOING TO BE OK

  75. WILL THIS BECOME PART OF PYTHON?

  76. PROBABLY (IN 3.8)

  77. BUT I DON'T LIKE IT!

  78. THEN DON'T WRITE IT!

  79. IS GUIDO COMING BACK?

  80. PROBABLY NOT

  81. WILL THIS HAPPEN AGAIN?

  82. PROBABLY NOT

  83. ¯\_(ϑ)_/¯

  84. THANKS!