PEP 498: The Monologue

A presentation at PyCon Australia 2017 in August 2017 in Melbourne VIC, Australia by Mariatta

Slide 1

Slide 1

PEP 498: the Monologue Mariatta Wijaya @mariatta PyCon Australia 2017 @pyconau

Slide 2

Slide 2

Who are you? Hello! !

Slide 3

Slide 3

Are you … … new to Python?

Slide 4

Slide 4

Are you … … only familiar with one PEP?

Slide 5

Slide 5

Are you … … using Python < 3.6?

Slide 6

Slide 6

Who am I?

Slide 7

Slide 7

PyLadies Vancouver Co-Organizer Python Core Developer @mariatta

Slide 8

Slide 8

What’s a PEP?

Slide 9

Slide 9

P ython E nhancement P roposal

Slide 10

Slide 10

PEP 1: PEP Purpose and Guideline https://www.python.org/dev/peps/pep-0001

Slide 11

Slide 11

" pitch to python-ideas

Slide 12

Slide 12

draft a PEP Official template: PEP 12 alt template: pep_cookiecutter $%

Slide 13

Slide 13

& BDFL Pronouncement ' (

Slide 14

Slide 14

429 PEPs today 82 rejected (~19%)

Slide 15

Slide 15

PEP 498

Slide 16

Slide 16

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

Slide 17

Slide 17

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

Slide 18

Slide 18

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

Slide 19

Slide 19

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

Slide 20

Slide 20

that escalated quickly ( 60+ replies later)

Slide 21

Slide 21

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

Slide 22

Slide 22

PEP 498: Literal String Formatting by: Eric V. Smith August 7th, 2015

Slide 23

Slide 23

PEP 498: Literal String Interpolation August 7th, 2015 August 30th, 2015 by: Eric V. Smith

Slide 24

Slide 24

PEP 498: Literal String Interpolation ready for pronouncement August 7th, 2015 August 30th, 2015 September 5th, 2015

Slide 25

Slide 25

PEP 498: Literal String Interpolation August 7th, 2015 August 30th, 2015 September 7th, 2015 September 5th, 2015

Slide 26

Slide 26

“The existing ways of formatting are either error prone , inflexible , or cumbersome .” RATIONALE

Slide 27

Slide 27

name = "Bart"

print( "Hello, %s ." % name) Hello, Bart.

Slide 28

Slide 28

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

Slide 29

Slide 29

name = "Bart"

age = 10

print( "Hello, %s . You’re %s ." %

( name, age ) ) Hello, Bart. You're 10.

Slide 30

Slide 30

PEP 3101: str.format https://www.python.org/dev/peps/pep-3101

Slide 31

Slide 31

name = "Bart"

age = 10

print( "Hello,

{name} . You’re {age} . " .format( name =name, age =age)) Hello, Bart. You're 10.

Slide 32

Slide 32

PEP 498: f-string https://www.python.org/dev/peps/pep-0498

Slide 33

Slide 33

name = "Bart"

age = 10

print( "Hello,

{name} . You’re {age} . " .format( name =name, age =age))

Slide 34

Slide 34

name = "Bart"

age = 10

print( "Hello,

{name} . You’re {age} . " )

Slide 35

Slide 35

name = "Bart"

age = 10

print( f "Hello, {name} . You’re

{age} ." ) Hello, Bart. You're 10.

Slide 36

Slide 36

Recap

Slide 37

Slide 37

"Hello, %s . You’re %s . " % (name, age ) "Hello, {name} . You’re {age} ." .format( name= name, age= age) f " Hello, {name} . You’re {age} . "

Slide 38

Slide 38

f " Hello, {name} . You’re {age} . "

expression

Slide 39

Slide 39

f " Hello, {name} . You’re {age} . "

literal

Slide 40

Slide 40

f "..."

✅ F' ...'

✅ f """..."""

Slide 41

Slide 41

r + f = fr " … " Raw f-strings

Slide 42

Slide 42

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

Slide 43

Slide 43

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

Slide 44

Slide 44

fr "..."

✅ RF "..."

✅ rF """..."""

Slide 45

Slide 45

fb "..."

❌ uf "..."

Slide 46

Slide 46

def to_uppercase(input): ...      return input.upper() ...

name = "bart simpson"

print( f"Hi {to_uppercase(name)} !" ) Hi BART SIMPSON!

Slide 47

Slide 47

pi = 3.14159265

print( "pi 3 decimal places %.3f " % pi )

3.142

print( f"pi 3 decimal places {pi:.3f} " )

3.142

Slide 48

Slide 48

number =

1024

print( f"hex: {number:#0x} " )

hex: 0x400

print( f"binary: {number:#0b} " )

binary: 0b10000000000

print( f"octal: {number:#0o} " )

octal: 0o2000

Slide 49

Slide 49

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} " )

  • 10

Slide 50

Slide 50

not in docstrings

def spam(): ...
f " doing stuff ”

...

spam. doc is None True bpo-28739

Slide 51

Slide 51

,

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

Slide 52

Slide 52

multiline strings

message = ( f "Dear {name} ,"

...             f ”You can win {prize:.2f} $"

...             f "Make a purchase before {tomorrow}:%Y-%b-%d " )

bpo-29668

Slide 53

Slide 53

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.

Slide 54

Slide 54

.

Documentation … ? /

Slide 55

Slide 55

.

Documentation https://docs.python.org/3/reference/lexical_analysis.html#formatted-string-literals ⭐ 1 Formatted String Literal 2 docs.python.org

glossary

f-string

Slide 56

Slide 56

timeit

Slide 57

Slide 57

$ python3 -mtimeit -s 'a= 2 ' "'%s' % a " 10000000 loops, best of 3: 0.197

usec per loop

Slide 58

Slide 58

$ 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

Slide 59

Slide 59

$ 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

Slide 60

Slide 60

$ 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

Slide 61

Slide 61

PEP 498

Slide 62

Slide 62

f-strings

Slide 63

Slide 63

Python 3.6 download at www.python.org

Slide 64

Slide 64

4

bonus!

Slide 65

Slide 65

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

Slide 66

Slide 66

Thank you! Mariatta Wijaya @mariatta | mariatta@python.org

File bugs at

https://bugs.python.org PyCon Australia 2017 @pyconau