A presentation at PyCon Australia 2017 in in Melbourne VIC, Australia by Mariatta
PEP 498: the Monologue Mariatta Wijaya @mariatta PyCon Australia 2017 @pyconau
Who are you? Hello! !
Are you … … new to Python?
Are you … … only familiar with one PEP?
Are you … … using Python < 3.6?
Who am I?
PyLadies Vancouver Co-Organizer Python Core Developer @mariatta
What’s a PEP?
P ython E nhancement P roposal
PEP 1: PEP Purpose and Guideline https://www.python.org/dev/peps/pep-0001
" pitch to python-ideas
draft a PEP Official template: PEP 12 alt template: pep_cookiecutter $%
& BDFL Pronouncement ' (
429 PEPs today 82 rejected (~19%)
PEP 498
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 Jul 20 01:12:31 CEST 2015 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
From: Eric V. Smith Subject: Re: [python-ideas] Briefer string format Jul 20 01:12:31 CEST 2015 Jul 20 01:27:42 CEST 2015 What would this do? https://mail.python.org/pipermail/python-ideas/2015-July/034658.html
From: C. A. Subject: Re: [python-ideas] Briefer string format Jul 20 01:12:31 CEST 2015 https://mail.python.org/pipermail/python-ideas/2015-July/034660.html I’m -1 on the specific idea, though definitely sympathetic to the broader concept of simplified formatting of strings. Jul 20 01:44:09 CEST 2015 Jul 20 01:27:42 CEST 2015
From: S. D. Subject: Re: [python-ideas] Briefer string format 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 Jul 20 01:27:42 CEST 2015 Jul 20 01:44:09 CEST 2015 Jul 20 02:43:29 CEST 2015 https://mail.python.org/pipermail/python-ideas/2015-July/034669.html
that escalated quickly ( 60+ replies later)
From: Guido van Rossum Subject: Re: [python-ideas] Briefer string format Jul 20 01:12:31 CEST 2015 Jul 20 01:27:42 CEST 2015 Jul 20 01:44:09 CEST 2015 Jul 20 02:43:29 CEST 2015 Jul 21 08:05:17 CEST 2015 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; https://mail.python.org/pipermail/python-ideas/2015-July/034729.html
PEP 498: Literal String Formatting by: Eric V. Smith August 7th, 2015
PEP 498: Literal String Interpolation August 7th, 2015 August 30th, 2015 by: Eric V. Smith
PEP 498: Literal String Interpolation ready for pronouncement August 7th, 2015 August 30th, 2015 September 5th, 2015
PEP 498: Literal String Interpolation August 7th, 2015 August 30th, 2015 September 7th, 2015 September 5th, 2015
“The existing ways of formatting are either error prone , inflexible , or cumbersome .” RATIONALE
name = "Bart"
print( "Hello, %s ." % name) Hello, Bart.
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
name = "Bart"
age = 10
print( "Hello, %s . You’re %s ." %
( name, age ) ) Hello, Bart. You're 10.
PEP 3101: str.format https://www.python.org/dev/peps/pep-3101
name = "Bart"
age = 10
print( "Hello,
{name} . You’re {age} . " .format( name =name, age =age)) Hello, Bart. You're 10.
PEP 498: f-string https://www.python.org/dev/peps/pep-0498
name = "Bart"
age = 10
print( "Hello,
{name} . You’re {age} . " .format( name =name, age =age))
name = "Bart"
age = 10
print( "Hello,
{name} . You’re {age} . " )
name = "Bart"
age = 10
print( f "Hello, {name} . You’re
{age} ." ) Hello, Bart. You're 10.
Recap
"Hello, %s . You’re %s . " % (name, age ) "Hello, {name} . You’re {age} ." .format( name= name, age= age) f " Hello, {name} . You’re {age} . "
f " Hello, {name} . You’re {age} . "
expression
f " Hello, {name} . You’re {age} . "
literal
f "..."
✅ F' ...'
✅ f """..."""
✅
✅
r + f = fr " … " Raw f-strings
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
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
fr "..."
✅ RF "..."
✅ rF """..."""
✅
fb "..."
❌ uf "..."
❌
def to_uppercase(input): ... return input.upper() ...
name = "bart simpson"
print( f"Hi {to_uppercase(name)} !" ) Hi BART SIMPSON!
pi = 3.14159265
print( "pi 3 decimal places %.3f " % pi )
3.142
print( f"pi 3 decimal places {pi:.3f} " )
3.142
number =
1024
print( f"hex: {number:#0x} " )
hex: 0x400
print( f"binary: {number:#0b} " )
binary: 0b10000000000
print( f"octal: {number:#0o} " )
octal: 0o2000
pycon_au = datetime ( year= 2017, month= 8, day= 5)
print( f" {pycon_au:%b %d, %Y} " )
Aug 05, 2017
print( f" {name:>20} " )
Bart
print( f" {age:=+5d} " )
❌
not in docstrings
def
spam():
...
f
"
doing stuff
”
...
spam. doc is None True bpo-28739
,
multiline strings?
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} " )
bpo-29668
✅
multiline strings
message = ( f "Dear {name} ,"
... f ”You can win {prize:.2f} $"
... f "Make a purchase before {tomorrow}:%Y-%b-%d " )
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.
.
Documentation … ? /
.
Documentation https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals ⭐ 1 Formatted String Literal 2 docs.python.org
glossary
f-string
⏱
timeit
$ 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' "'%s' % a" 10000000 loops, best of 3: 0.197
usec per loop
$ python3 -mtimeit -s 'a= 2 ' ' f" {a} " ' 10000000 loops, best of 3: 0.105 usec per loop $ python3 -mtimeit -s 'a=2' "'%s' % a" $ python3 -mtimeit -s 'a=2' '"{}".format(a)' 1000000 loops, best of 3:
0.341
usec per loop 10000000 loops, best of 3: 0.197
usec per loop
$ python3 -mtimeit -s 'a=2' ' f" {a} " ' 10000000 loops, best of 3: 0.105
usec per loop $ python3 -mtimeit -s 'a=2' "'%s' % a" $ python3 -mtimeit -s 'a=2' '"{}".format(a)' 1000000 loops, best of 3:
0.341
usec per loop 10000000 loops, best of 3: 0.197
usec per loop
PEP 498
f-strings
Python 3.6 download at www.python.org
4
bonus!
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
Thank you! Mariatta Wijaya @mariatta | mariatta@python.org
File bugs at
https://bugs.python.org PyCon Australia 2017 @pyconau
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!