A presentation at PyCon Canada 2017 in in Montreal, QC, Canada by Mariatta
MARIATTA WIJAYA @mariatta PYCON CANADA 2017 PEP 498: THE MONOLOGUE #PEPtalk
! You cool people! Who are you? @mariatta PYCON CANADA 2017 #PEPtalk
Are you … … new to Python? @mariatta PYCON CANADA 2017 #PEPtalk
Are you … … only aware of one PEP? @mariatta PYCON CANADA 2017 #PEPtalk
Are you … … using Python < 3.6? @mariatta PYCON CANADA 2017 #PEPtalk
Who am I? @mariatta PYCON CANADA 2017 #PEPtalk
Who am I? @mariatta PYCON CANADA 2017 #PEPtalk
Who am I? @mariatta @mariatta PYCON CANADA 2017 #PEPtalk
Who am I? @mariatta @mariatta PYCON CANADA 2017 #PEPtalk
Who am I? @mariatta Software Engineer @mariatta PYCON CANADA 2017 #PEPtalk
Who am I? @mariatta Software Engineer Python Core Developer @mariatta PYCON CANADA 2017 #PEPtalk
What’s a PEP? @mariatta PYCON CANADA 2017 #PEPtalk
P ython E nhancement P roposal @mariatta PYCON CANADA 2017 #PEPtalk
PEP 1 PEP Purpose and Guideline https://www.python.org/dev/peps/pep-0001 @mariatta PYCON CANADA 2017 #PEPtalk
"
pitch to python-ideas @mariatta PYCON CANADA 2017 #PEPtalk
draft a PEP o ffi cial template: PEP 12 @mariatta PYCON CANADA 2017 #PEPtalk
$
BDFL Pronouncement @mariatta PYCON CANADA 2017 #PEPtalk
$
BDFL Pronouncement % @mariatta PYCON CANADA 2017 #PEPtalk
$
BDFL Pronouncement % & @mariatta PYCON CANADA 2017 #PEPtalk
450 PEPs today 88 rejected @mariatta PYCON CANADA 2017 #PEPtalk
PEP 498 @mariatta PYCON CANADA 2017 #PEPtalk
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
Jul 20 01:12:31 CEST 2015 H a v e l o n g w i s h e d p y t h o n c o u l d f o r m a t s t r i n g s e a s i l y l i k e b a s h o r p e r l d o … a n d t h e n i t h i t m e 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
f ' { n l } { s e l e c t o r } { s p a c e } { { { n l } ' https://mail.python.org/pipermail/python-ideas/2015-July/034657.html @mariatta PYCON CANADA 2017 #PEPtalk
f ' { n l } { s e l e c t o r } { s p a c e } { { { n l } ' I ' v e s e e n o t h e r s m a k e s i m i l a r s u g g e s t i o n s , b u t t o m y k n o w l e d g e t h e y d i d n ’ t i n c l u d e t h i s p l e a s i n g b r e v i t y a s p e c t . https://mail.python.org/pipermail/python-ideas/2015-July/034657.html @mariatta PYCON CANADA 2017 #PEPtalk
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
Jul 20 01:12:31 CEST 2015 W h a t w o u l d t h i s d o ? 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
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
1 o n t h e s p e c ific i d e a , t h o u g h d efin i t e l y s y m p a t h e t i c t o t h e b r o a d e r c o n c e p t o f s i m p l ifie d f o r m a t t i n g o f s t r i n g s . 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
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
d o n ’ t e v e n h a v e t o m o d i f y t h e s t r i n g l i t e r a l . 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
that escalated quickly @mariatta PYCON CANADA 2017 #PEPtalk
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
T h a n k s , E r i c ! Y o u ’ r e a d d r e s s i n g a l l m y c o n c e r n s a n d y o u ’ r e g o i n g e x a c t l y w h e r e I w a n t e d t h i s t o g o . I h o p e t h a t y o u w i l lfin d t h e t i m e t o w r i t e u p a P E P ; 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
By: Eric V. Smith PEP 498: Literal String Formatting August 7th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
By: Eric V. Smith PEP 498: Literal String Interpolation August 7th, 2015 August 30th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
ready for pronouncement PEP 498: Literal String Interpolation 1 August 7th, 2015 August 30th, 2015 September 5th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
PEP 498: Literal String Interpolation August 7th, 2015 August 30th, 2015 September 5th, 2015 September 7th, 2015 @mariatta PYCON CANADA 2017 #PEPtalk
“The existing ways of formatting are either error prone , inflexible , or cumbersome .” RATIONALE @mariatta PYCON CANADA 2017 #PEPtalk
name = "Bart"
print( "Hello, %s ." % name) Hello, Bart. @mariatta PYCON CANADA 2017 #PEPtalk
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
name = "Bart"
age = 10
print( "Hello, %s . You’re %s ." %
( name, age ) ) Hello, Bart. You're 10. @mariatta PYCON CANADA 2017 #PEPtalk
PEP 3101: str.format https://www.python.org/dev/peps/pep-31 01 @mariatta PYCON CANADA 2017 #PEPtalk
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
PEP 498: f-string https://www.python.org/dev/peps/pep- 0498 @mariatta PYCON CANADA 2017 #PEPtalk
name = "Bart"
age = 10
print( "Hello,
{name} . You’re {age} . " .format( name =name, age =age)) @mariatta PYCON CANADA 2017 #PEPtalk
name = "Bart"
age = 10
print( "Hello,
{name} . You’re {age} . " ) @mariatta PYCON CANADA 2017 #PEPtalk
name = "Bart"
age = 10
print( f "Hello, {name} . You’re
{age} ." ) @mariatta PYCON CANADA 2017 #PEPtalk
name = "Bart"
age = 10
print( f "Hello, {name} . You’re
{age} ." ) @mariatta PYCON CANADA 2017 #PEPtalk Hello, Bart. You're 10.
recap @mariatta PYCON CANADA 2017 #PEPtalk
"Hello, %s . You’re %s . " % (name, age ) @mariatta PYCON CANADA 2017 #PEPtalk
"Hello, %s . You’re %s . " % (name, age ) "Hello, {name} . You’re {age} ." .format( name= name, age= age) @mariatta PYCON CANADA 2017 #PEPtalk
"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
f " Hello, {name} . You’re {age} . "
Expression @mariatta PYCON CANADA 2017 #PEPtalk
f " Hello, {name} . You’re {age} . "
Literal @mariatta PYCON CANADA 2017 #PEPtalk
f " Hello, world. "
f-string @mariatta PYCON CANADA 2017 #PEPtalk
f "..."
✅ F' ...'
✅ f """..."""
✅ @mariatta PYCON CANADA 2017 #PEPtalk
✅
r + f = fr " … " Raw f-strings @mariatta PYCON CANADA 2017 #PEPtalk
print( "The smiley face emoji is \U0001f600" ) @mariatta PYCON CANADA 2017 #PEPtalk
print( "The smiley face emoji is \U0001f600" ) The smiley face emoji is ( @mariatta PYCON CANADA 2017 #PEPtalk
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
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
code = "emoji "
print( f"The smiley face {code} is \U0001f600" ) @mariatta PYCON CANADA 2017 #PEPtalk
code = "emoji "
print( f"The smiley face {code} is \U0001f600" ) The smiley face emoji is ( @mariatta PYCON CANADA 2017 #PEPtalk
code = "emoji "
print( f"The smiley face {code} is \U0001f600" ) The smiley face emoji is (
code = "unicode "
print( f r "The smiley face {code} is \U0001f600" ) @mariatta PYCON CANADA 2017 #PEPtalk
code = "emoji "
print( f"The smiley face {code} is \U0001f600" ) The smiley face emoji is (
code = "unicode "
print( f r "The smiley face {code} is \U0001f600" ) The smiley face unicode is \U0001f600 @mariatta PYCON CANADA 2017 #PEPtalk
rF """..."""
✅ fr "..."
✅ RF "..."
✅ PYCON CANADA 2017 @mariatta PYCON CANADA 2017 #PEPtalk
fb "..."
❌ uf "..."
❌ @mariatta PYCON CANADA 2017 #PEPtalk
name = "bart simpson"
print( f"Hi {name.upper()} !” ) Hi BART SIMPSON!
@mariatta PYCON CANADA 2017 #PEPtalk
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
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
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
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} " )
@mariatta PYCON CANADA 2017 #PEPtalk
❌
Not in Docstrings
def
spam():
...
f
"
doing stuff
"
...
spam. doc is None True bpo-28739 @mariatta PYCON CANADA 2017 #PEPtalk
Multi-line S trings? 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
✅
Multi-line S trings
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
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
,
Documentation … ? Python 3.6.2 @mariatta PYCON CANADA 2017 #PEPtalk
,
Python 3.6.3 @mariatta PYCON CANADA 2017 #PEPtalk
,
Documentation https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals ⭐ / Formatted String Literal 0 docs.python.org
glossary > f-string @mariatta PYCON CANADA 2017 #PEPtalk
⏱
timeit @mariatta PYCON CANADA 2017 #PEPtalk
$ python3 -mtimeit -s 'a= 2 ' "'%s' % a " 10000000 loops, best of 3:
0.197
usec per loop @mariatta PYCON CANADA 2017 #PEPtalk
$ 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
$ 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
$ 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
PEP 498 @mariatta PYCON CANADA 2017 #PEPtalk
f-strings @mariatta PYCON CANADA 2017 #PEPtalk
Python 3.6 download at www.python.org @mariatta PYCON CANADA 2017 #PEPtalk
Python 3.6 download at www.python.org @mariatta PYCON CANADA 2017 #PEPtalk
2
bonus! @mariatta PYCON CANADA 2017 #PEPtalk
16 PEPs included @mariatta PYCON CANADA 2017 #PEPtalk
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
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
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
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
Thank you! file issues at bugs.python.org Mariatta Wijaya | mariatta@python.org
@mariatta PYCON CANADA 2017 #PEPtalk
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!