switch to python 3…now…immediately

56
Copyright © 2013 Russel Winder 1 Switch to Python 3… Now… Immediately! Prof Russel Winder http://www.russel.org.uk email: [email protected] xmpp: [email protected] twitter: @russel_winder

Upload: russel-winder

Post on 19-May-2015

2.003 views

Category:

Technology


0 download

DESCRIPTION

A presentation to The London Python Group (TLPG) 2013-02-12. A polemic against those who will not upgrade to Python 3.

TRANSCRIPT

Page 1: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 1

Switch to Python 3…Now…

Immediately!

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder

Page 2: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 2

Interstitial Advertisement

Page 3: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 3

polemic noun a strong verbal or written attack on someone or something

Page 4: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 4

Introduction

Page 5: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 5

2.72.62.5

2.4

2.3

2.2

Page 6: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 6

3.33.2

3.13.0

Page 7: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 7

gs/2\../3.3/g

Page 8: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 8

So what's the problem?

● Python 2 remains the default Python.● Too many people and organizations are far too

afraid of change.

Why do people and organizationsbecome so afraid of change?

Page 9: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 9

So what's the solution?

● Python Software Foundation (PSF) should declare Python 3 the default Python immediately.

People and organizations can implement this now:python python2→

replaced by:python python3→

Page 10: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 10

But…world, we have a problem…

● …a few influential organizations are telling people not to use Python 3 but to stay with Python 2, and…

● …some crucial projects can't or won't get Python 3 versions out.

Many projects are now both Python 2 andPython 3 or getting there very rapidly.

Afraid of change?

Afraid of change?

Page 11: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 11

Recalcitrant organizations and projects

should just pull their fingers out and

set Python 3 as their standard.

Page 12: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 12

Or at least ensure their codebases

run under both Python 2 and Python 3.

Page 13: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 13

Being Python 2 and 3 compatible

● 2to3 not useful, its a one shot transform tool.● six package might be useful in parts, and/or…● …modernize package might help, but…● …is manual the best?

Page 14: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 14

Embrace

managed and controlled

change.

Page 15: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 15

Be agile.

Page 16: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 16

Justifications

Page 17: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 17

Python 2 is a dead end.

Python 3 is the developing future.

Page 18: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 18

print(x)

exec(x)

Page 19: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 19

from __future__ import print_function

Page 20: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 20

from __future__ import ( division, absolute_import, print_function, unicode_literals,)

Page 21: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 21

x = [x * x for x in range(100) if x % 10 == 0]

Page 22: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 22

x = {x * x for x in range(100) if x % 10 == 0}

Page 23: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 23

x = {x: x * x for x in range(100) if x % 10 == 0}

Page 24: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 24

Sadly (!) Python 2.7 is getting

backports of things from Python 3.

Page 25: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 25

Fortunately Python 2.6

and earlier are not.

Page 26: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 26

Python 2 is strict,

Python 3 is (sort of) lazy

Page 27: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 27

map(lambda x: x * x, filter(lambda x: x % 10 == 0, range(100))

Page 28: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 28

range(x)

xrange(x)

tuple(range(x))

range(x)→

Page 29: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 29

Not Sequences, but Iterators

items

keys

values

iteritems

iterkeys

itervalues

items

keys

values

Page 30: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 30

Competition

Page 31: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 31

Python is under challenge

● Go is taking over from Python for many people.● Native, not interpreted.● Solid concurrency model – CSP

● D is hoping to take over from C, C++, Go, Python.● Native, not interpreted.● Solid concurrency models – actors, data parallelism

Page 32: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 32

Native code languages are now

as expressive as Python.

Page 33: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 33

And native code programs are

way, way faster than Python programs.

Page 34: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 34

Statically compiled languages catch errors at

compile time that are horrendous run

time bugs in dynamic languages.

Page 35: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 35

Are you concurrent?

● Who's using thread?● Who's using threading?

Page 36: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 36

Are you parallel?

● Who's using thread?● Who's using threading?● Who's using multiprocessing?

Page 37: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 37

At least IronPython and Jython

do not have a GIL as

CPython and PyPy do.

Page 38: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 38

Support the PyPy STM/AME experiment.

Page 39: Switch to Python 3…now…immediately

Copyright © 2011–2012 Russel Winder 39

NB CSP stands for

Concurrent Sequential Processes

Page 40: Switch to Python 3…now…immediately

Copyright © 2011–2012 Russel Winder 40

ActorsIndependent processes communicating via asynchronous exchange of messages

CSPSequential processes connected by channels using synchronous message exchange (rendezvous).

DataflowOperators connected by channels with activity triggered by arrival of data on the channels.

Page 41: Switch to Python 3…now…immediately

Copyright © 2011–2012 Russel Winder 41

ActorsIndependent processes communicating via asynchronous exchange of messages

Page 42: Switch to Python 3…now…immediately

Copyright © 2011–2012 Russel Winder 42

DataflowOperators connected by channels with activity triggered by arrival of data on the channels.

Page 43: Switch to Python 3…now…immediately

Copyright © 2011–2012 Russel Winder 43

CSPSequential processes connected by channels using synchronous message exchange (rendezvous).

Page 44: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 44

Data ParallelismTransform a sequence to another sequence where all individual actions happen at the same time.

Page 45: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 45

Futures are the future.

Page 46: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 46

Along with process pools

and thread pools.

Page 47: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 47

Concurrent.futures

● Python 3.2 package to abstract over threads and processes to give an asynchronous function call and future system.

cf. futures package for Python 2.

Page 48: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 48

Talking of PyPy…

Page 49: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 49

Support PyPy for Python 3.

Page 50: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 50

Conclusion

Page 51: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 51

There are no real reasons

to stay with Python 2…

Page 52: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 52

…so Python 3 should be

Considered mandatory.

Page 53: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 53

Challenge

Page 54: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 54

Problem

Python 3Python 2

Is there a problem, the solution to which canbe coded in Python 2, but not better in Python 3?

Page 55: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 55

Interstitial Advertisement

Page 56: Switch to Python 3…now…immediately

Copyright © 2013 Russel Winder 56

Switch to Python 3…Now…

Immediately!

Prof Russel Winder

http://www.russel.org.uk

email: [email protected]: [email protected]

twitter: @russel_winder