PEP 498: The Monologue

6b6e72d297aa0270654a0d4575f1287e?s=47 Mariatta
November 19, 2017

PEP 498: The Monologue

Video: https://youtu.be/kGd1TVfP_Ds

One of the exciting new features in Python 3.6, PEP 498 is also a case study of a successful Python Enhancement Proposal and implementation.

Get a glimpse of a lifecycle of a PEP. Learn about f-strings, see some examples, and know the gotchas. You’ll want to upgrade to Python 3.6 just for this!

Presented as the closing keynote at PyCon Canada 2017.
Also presented at PyCon Australia 2017.

6b6e72d297aa0270654a0d4575f1287e?s=128

Mariatta

November 19, 2017
Tweet

Transcript

  1. MARIATTA WIJAYA @mariatta PYCON CANADA 2017 PEP 498: THE MONOLOGUE

    #PEPtalk
  2. You cool people! Who are you? @mariatta PYCON CANADA 2017

    #PEPtalk
  3. Are you … … new to Python? @mariatta PYCON CANADA

    2017 #PEPtalk
  4. Are you … … only aware of one PEP? @mariatta

    PYCON CANADA 2017 #PEPtalk
  5. Are you … … using Python < 3.6? @mariatta PYCON

    CANADA 2017 #PEPtalk
  6. Who am I? @mariatta PYCON CANADA 2017 #PEPtalk

  7. Who am I? @mariatta PYCON CANADA 2017 #PEPtalk

  8. Who am I? @mariatta @mariatta PYCON CANADA 2017 #PEPtalk

  9. Who am I? @mariatta @mariatta PYCON CANADA 2017 #PEPtalk

  10. Who am I? @mariatta Software Engineer @mariatta PYCON CANADA 2017

    #PEPtalk
  11. Who am I? @mariatta Software Engineer Python Core Developer @mariatta

    PYCON CANADA 2017 #PEPtalk
  12. What’s a PEP? @mariatta PYCON CANADA 2017 #PEPtalk

  13. Python Enhancement Proposal @mariatta PYCON CANADA 2017 #PEPtalk

  14. PEP 1 PEP Purpose and Guideline https://www.python.org/dev/peps/pep-0001 @mariatta PYCON CANADA

    2017 #PEPtalk
  15. pitch to python-ideas @mariatta PYCON CANADA 2017 #PEPtalk

  16. draft a PEP official template: PEP 12 @mariatta PYCON CANADA

    2017 #PEPtalk
  17. BDFL Pronouncement @mariatta PYCON CANADA 2017 #PEPtalk

  18. BDFL Pronouncement @mariatta PYCON CANADA 2017 #PEPtalk

  19. BDFL Pronouncement @mariatta PYCON CANADA 2017 #PEPtalk

  20. 450 PEPs today 88 rejected @mariatta PYCON CANADA 2017 #PEPtalk

  21. PEP 498 @mariatta PYCON CANADA 2017 #PEPtalk

  22. Jul 20 01:12:31 CEST 2015 From: Mike Miller Subject: [python-ideas]

    Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034657.html @mariatta PYCON CANADA 2017 #PEPtalk
  23. Jul 20 01:12:31 CEST 2015 Have long wished python could

    format strings easily like bash or perl do … and then it hit me From: Mike Miller Subject: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034657.html @mariatta PYCON CANADA 2017 #PEPtalk
  24. Jul 20 01:12:31 CEST 2015 Have long wished python could

    format strings easily like bash or perl do … and then it hit me From: Mike Miller Subject: [python-ideas] Briefer string format csstext += f'{nl}{selector}{space}{{{nl}' https://mail.python.org/pipermail/python-ideas/2015-July/034657.html @mariatta PYCON CANADA 2017 #PEPtalk
  25. Jul 20 01:12:31 CEST 2015 Have long wished python could

    format strings easily like bash or perl do … and then it hit me From: Mike Miller Subject: [python-ideas] Briefer string format csstext += f'{nl}{selector}{space}{{{nl}' I've seen others make similar suggestions, but to my knowledge they didn’t include this pleasing brevity aspect. https://mail.python.org/pipermail/python-ideas/2015-July/034657.html @mariatta PYCON CANADA 2017 #PEPtalk
  26. Jul 20 01:12:31 CEST 2015 From: Eric V. Smith Subject:

    Re: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034658.html Jul 20 01:27:42 CEST @mariatta PYCON CANADA 2017 #PEPtalk
  27. Jul 20 01:12:31 CEST 2015 What would this do? From:

    Eric V. Smith Subject: Re: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034658.html Jul 20 01:27:42 CEST @mariatta PYCON CANADA 2017 #PEPtalk
  28. Jul 20 01:12:31 CEST 2015 From: C. A. Subject: Re:

    [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034660.html Jul 20 01:27:42 CEST Jul 20 01:44:09 CEST @mariatta PYCON CANADA 2017 #PEPtalk
  29. I’m -1 on the specific idea, though definitely sympathetic to

    the broader concept of simplified formatting of strings. Jul 20 01:12:31 CEST 2015 From: C. A. Subject: Re: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034660.html Jul 20 01:27:42 CEST Jul 20 01:44:09 CEST @mariatta PYCON CANADA 2017 #PEPtalk
  30. Jul 20 01:12:31 CEST 2015 From: S. D. Subject: Re:

    [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034669.html Jul 20 01:27:42 CEST @mariatta PYCON CANADA 2017 #PEPtalk Jul 20 02:43:29 CEST Jul 20 01:44:09 CEST
  31. It’s syntactic sugar for a simple function call with perfectly

    well defined semantics - don’t even have to modify the string literal. I’m +1. Jul 20 01:12:31 CEST 2015 From: S. D. Subject: Re: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034669.html Jul 20 01:27:42 CEST @mariatta PYCON CANADA 2017 #PEPtalk Jul 20 02:43:29 CEST Jul 20 01:44:09 CEST
  32. that escalated quickly @mariatta PYCON CANADA 2017 #PEPtalk

  33. Jul 20 01:12:31 CEST 2015 From: Guido van Rossum Subject:

    Re: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034729.html Jul 20 01:27:42 CEST Jul 20 01:44:09 CEST Jul 20 02:43:29 CEST Jul 21 08:05:15 CEST @mariatta PYCON CANADA 2017 #PEPtalk
  34. Thanks, Eric! You’re addressing all my concerns and you’re going

    exactly where I wanted this to go. I hope that you will find the time to write up a PEP; Jul 20 01:12:31 CEST 2015 From: Guido van Rossum Subject: Re: [python-ideas] Briefer string format https://mail.python.org/pipermail/python-ideas/2015-July/034729.html Jul 20 01:27:42 CEST Jul 20 01:44:09 CEST Jul 20 02:43:29 CEST Jul 21 08:05:15 CEST @mariatta PYCON CANADA 2017 #PEPtalk
  35. By: Eric V. Smith PEP 498: Literal String Formatting August

    7th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
  36. By: Eric V. Smith PEP 498: Literal String Interpolation August

    7th, 2015 August 30th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
  37. ready for pronouncement PEP 498: Literal String Interpolation 1 August

    7th, 2015 August 30th, 2015 September 5th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
  38. PEP 498: Literal String Interpolation August 7th, 2015 August 30th,

    2015 September 5th, 2015 September 7th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
  39. “The existing ways of formatting are either error prone, inflexible,

    or cumbersome.” RATIONALE @mariatta PYCON CANADA 2017 #PEPtalk
  40. >>> name = "Bart" >>> print("Hello, %s." % name) Hello,

    Bart. @mariatta PYCON CANADA 2017 #PEPtalk
  41. >>> name = "Bart" >>> age = 10 >>> print("Hello,

    %s. You’re %s." % name, age) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: not enough arguments for format string @mariatta PYCON CANADA 2017 #PEPtalk
  42. >>> name = "Bart" >>> age = 10 >>> print("Hello,

    %s. You’re %s." % (name, age)) Hello, Bart. You're 10. @mariatta PYCON CANADA 2017 #PEPtalk
  43. PEP 3101: str.format https://www.python.org/dev/peps/pep-3101 @mariatta PYCON CANADA 2017 #PEPtalk

  44. >>> name = "Bart" >>> age = 10 >>> print("Hello,

    {name}. You’re {age}.".format(name=name, age=age)) Hello, Bart. You're 10. @mariatta PYCON CANADA 2017 #PEPtalk
  45. PEP 498: f-string https://www.python.org/dev/peps/pep-0498 @mariatta PYCON CANADA 2017 #PEPtalk

  46. >>> name = "Bart" >>> age = 10 >>> print("Hello,

    {name}. You’re {age}.".format(name=name, age=age)) @mariatta PYCON CANADA 2017 #PEPtalk
  47. >>> name = "Bart" >>> age = 10 >>> print("Hello,

    {name}. You’re {age}.") @mariatta PYCON CANADA 2017 #PEPtalk
  48. >>> name = "Bart" >>> age = 10 >>> print(f"Hello,

    {name}. You’re {age}.") @mariatta PYCON CANADA 2017 #PEPtalk
  49. >>> name = "Bart" >>> age = 10 >>> print(f"Hello,

    {name}. You’re {age}.") @mariatta PYCON CANADA 2017 #PEPtalk Hello, Bart. You're 10.
  50. recap @mariatta PYCON CANADA 2017 #PEPtalk

  51. "Hello, %s. You’re %s." % (name, age) @mariatta PYCON CANADA

    2017 #PEPtalk
  52. "Hello, %s. You’re %s." % (name, age) "Hello, {name}. You’re

    {age}.".format(name=name, age=age) @mariatta PYCON CANADA 2017 #PEPtalk
  53. "Hello, %s. You’re %s." % (name, age) "Hello, {name}. You’re

    {age}.".format(name=name, age=age) f"Hello, {name}. You’re {age}." @mariatta PYCON CANADA 2017 #PEPtalk
  54. f"Hello, {name}. You’re {age}." Expression @mariatta PYCON CANADA 2017 #PEPtalk

  55. f"Hello, {name}. You’re {age}." Literal @mariatta PYCON CANADA 2017 #PEPtalk

  56. f"Hello, world." f-string @mariatta PYCON CANADA 2017 #PEPtalk

  57. f"..." ✅ F'...' ✅ f"""...""" ✅ @mariatta PYCON CANADA 2017

    #PEPtalk
  58. ✅ r + f = fr"…" Raw f-strings @mariatta PYCON

    CANADA 2017 #PEPtalk
  59. >>> print("The smiley face emoji is \U0001f600") @mariatta PYCON CANADA

    2017 #PEPtalk
  60. >>> print("The smiley face emoji is \U0001f600") The smiley face

    emoji is @mariatta PYCON CANADA 2017 #PEPtalk
  61. >>> print("The smiley face emoji is \U0001f600") The smiley face

    emoji is >>> print(r"The smiley face unicode is \U0001f600") @mariatta PYCON CANADA 2017 #PEPtalk
  62. >>> print("The smiley face emoji is \U0001f600") The smiley face

    emoji is >>> print(r"The smiley face unicode is \U0001f600") The smiley face unicode is \U0001f600 @mariatta PYCON CANADA 2017 #PEPtalk
  63. >>> code = "emoji" >>> print(f"The smiley face {code} is

    \U0001f600") @mariatta PYCON CANADA 2017 #PEPtalk
  64. >>> code = "emoji" >>> print(f"The smiley face {code} is

    \U0001f600") The smiley face emoji is @mariatta PYCON CANADA 2017 #PEPtalk
  65. >>> code = "emoji" >>> print(f"The smiley face {code} is

    \U0001f600") The smiley face emoji is >>> code = "unicode" >>> print(fr"The smiley face {code} is \U0001f600") @mariatta PYCON CANADA 2017 #PEPtalk
  66. >>> code = "emoji" >>> print(f"The smiley face {code} is

    \U0001f600") The smiley face emoji is >>> code = "unicode" >>> print(fr"The smiley face {code} is \U0001f600") The smiley face unicode is \U0001f600 @mariatta PYCON CANADA 2017 #PEPtalk
  67. rF"""...""" ✅ fr"..." ✅ RF"..." ✅ PYCON CANADA 2017 @mariatta

    PYCON CANADA 2017 #PEPtalk
  68. fb"..." ❌ uf"..." ❌ @mariatta PYCON CANADA 2017 #PEPtalk

  69. >>> name = "bart simpson" >>> print(f"Hi {name.upper()}!”) Hi BART

    SIMPSON! @mariatta PYCON CANADA 2017 #PEPtalk
  70. >>> def how_many_tacos(people): ... return people * 3 ... >>>

    people = 5 >>> print(f"Order {how_many_tacos(people)} tacos for meetup!") Order 15 tacos for meetup! @mariatta PYCON CANADA 2017 #PEPtalk
  71. >>> pi = 3.14159265 >>> print("pi 3 decimal places %.3f"

    % pi) 3.142 >>> print(f"pi 3 decimal places {pi:.3f}") 3.142 @mariatta PYCON CANADA 2017 #PEPtalk
  72. >>> number = 1024 >>> print(f"hex: {number:#0x}") hex: 0x400 >>>

    print(f"binary: {number:#0b}") binary: 0b10000000000 >>> print(f"octal: {number:#0o}") octal: 0o2000 @mariatta PYCON CANADA 2017 #PEPtalk
  73. >>> pycon_ca = datetime(year=2017, month=11, day=19) >>> print(f"{pycon_ca:%b %d, %Y}")

    Nov 19, 2017 >>> print(f"{name:>20}") Bart >>> print(f"{age:=+5d}") + 10 @mariatta PYCON CANADA 2017 #PEPtalk
  74. ❌ Not in Docstrings >>> def spam(): ... f"doing stuff"

    ... >>> spam.__doc__ is None True bpo-28739 @mariatta PYCON CANADA 2017 #PEPtalk
  75. Multi-line Strings? bpo-29668 >>> name = "Bart" >>> prize =

    50 >>> tomorrow = today() + timedelta(days=1) >>> message = (f"Dear {name}," ... "You can win {prize:.2f}$" ... "Make a purchase before {tomorrow:%Y-%b-%d}") @mariatta PYCON CANADA 2017 #PEPtalk
  76. ✅ Multi-line Strings >>> message = (f"Dear {name}," ... f"You

    can win {prize:.2f}$" ... f"Make a purchase before {tomorrow}:%Y-%b-%d") @mariatta PYCON CANADA 2017 #PEPtalk bpo-29668
  77. IDLE Needs Syntax Highlighting bpo-29287 • Needs separate colorization to

    make the expression distinct from the rest of the string. • Needs close-brace matching. • Would be desirable to have autocompletion as well. @mariatta PYCON CANADA 2017 #PEPtalk
  78. Documentation … ? Python 3.6.2 @mariatta PYCON CANADA 2017 #PEPtalk

  79. Documentation … ? - Python 3.6.3 @mariatta PYCON CANADA 2017

    #PEPtalk
  80. Documentation https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals ⭐ Formatted String Literal docs.python.org > glossary >

    f-string @mariatta PYCON CANADA 2017 #PEPtalk
  81. ⏱ timeit @mariatta PYCON CANADA 2017 #PEPtalk

  82. $ python3 -mtimeit -s 'a=2' "'%s' % a" 10000000 loops,

    best of 3: 0.197 usec per loop @mariatta PYCON CANADA 2017 #PEPtalk
  83. $ python3 -mtimeit -s 'a=2' "'%s' % a" 10000000 loops,

    best of 3: 0.197 usec per loop $ python3 -mtimeit -s 'a=2' '"{}".format(a)' 1000000 loops, best of 3: 0.341 usec per loop @mariatta PYCON CANADA 2017 #PEPtalk
  84. $ python3 -mtimeit -s 'a=2' "'%s' % a" 10000000 loops,

    best of 3: 0.197 usec per loop $ python3 -mtimeit -s 'a=2' '"{}".format(a)' 1000000 loops, best of 3: 0.341 usec per loop $ python3 -mtimeit -s 'a=2' 'f"{a}"' 10000000 loops, best of 3: 0.105 usec per loop @mariatta PYCON CANADA 2017 #PEPtalk
  85. $ python3 -mtimeit -s 'a=2' "'%s' % a" 10000000 loops,

    best of 3: 0.197 usec per loop $ python3 -mtimeit -s 'a=2' '"{}".format(a)' 1000000 loops, best of 3: 0.341 usec per loop $ python3 -mtimeit -s 'a=2' 'f"{a}"' 10000000 loops, best of 3: 0.105 usec per loop @mariatta PYCON CANADA 2017 #PEPtalk
  86. PEP 498 @mariatta PYCON CANADA 2017 #PEPtalk

  87. f-strings @mariatta PYCON CANADA 2017 #PEPtalk

  88. Python 3.6 download at www.python.org @mariatta PYCON CANADA 2017 #PEPtalk

  89. Python 3.6 download at www.python.org @mariatta PYCON CANADA 2017 #PEPtalk

  90. bonus! @mariatta PYCON CANADA 2017 #PEPtalk

  91. 16 PEPs included @mariatta PYCON CANADA 2017 #PEPtalk

  92. 16 PEPs included PEP 468 PEP 487 PEP 495 PEP

    498 PEP 506 PEP 509 PEP 515 PEP 519 PEP 520 PEP 523 PEP 524 PEP 525 PEP 526 PEP 528 PEP 529 PEP 530 @mariatta PYCON CANADA 2017 #PEPtalk
  93. 16 PEPs included PEP 468 PEP 487 PEP 495 PEP

    498 PEP 506 PEP 509 PEP 515 PEP 519 PEP 520 PEP 523 PEP 524 PEP 525 PEP 526 PEP 528 PEP 529 PEP 530 @mariatta PYCON CANADA 2017 #PEPtalk
  94. 16 PEPs included PEP 468 PEP 487 PEP 495 PEP

    498 PEP 506 PEP 509 PEP 515 PEP 519 PEP 520 PEP 523 PEP 524 PEP 525 PEP 526 PEP 528 PEP 529 PEP 530 @mariatta PYCON CANADA 2017 #PEPtalk 17
  95. 16 PEPs included PEP 468 PEP 487 PEP 495 PEP

    498 PEP 506 PEP 509 PEP 515 PEP 519 PEP 520 PEP 523 PEP 524 PEP 525 PEP 526 PEP 528 PEP 529 PEP 530 @mariatta PYCON CANADA 2017 #PEPtalk 17 PEP 628: add math.tau
  96. Thank you! file issues at bugs.python.org Mariatta Wijaya | mariatta@python.org

    @mariatta PYCON CANADA 2017 #PEPtalk