switch to python 3…now…immediately
DESCRIPTION
A presentation to The London Python Group (TLPG) 2013-02-12. A polemic against those who will not upgrade to Python 3.TRANSCRIPT
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
Copyright © 2013 Russel Winder 2
Interstitial Advertisement
Copyright © 2013 Russel Winder 3
polemic noun a strong verbal or written attack on someone or something
Copyright © 2013 Russel Winder 4
Introduction
Copyright © 2013 Russel Winder 5
2.72.62.5
2.4
2.3
2.2
Copyright © 2013 Russel Winder 6
3.33.2
3.13.0
Copyright © 2013 Russel Winder 7
gs/2\../3.3/g
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?
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→
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?
Copyright © 2013 Russel Winder 11
Recalcitrant organizations and projects
should just pull their fingers out and
set Python 3 as their standard.
Copyright © 2013 Russel Winder 12
Or at least ensure their codebases
run under both Python 2 and Python 3.
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?
Copyright © 2013 Russel Winder 14
Embrace
managed and controlled
change.
Copyright © 2013 Russel Winder 15
Be agile.
Copyright © 2013 Russel Winder 16
Justifications
Copyright © 2013 Russel Winder 17
Python 2 is a dead end.
Python 3 is the developing future.
Copyright © 2013 Russel Winder 18
print(x)
exec(x)
Copyright © 2013 Russel Winder 19
from __future__ import print_function
Copyright © 2013 Russel Winder 20
from __future__ import ( division, absolute_import, print_function, unicode_literals,)
Copyright © 2013 Russel Winder 21
x = [x * x for x in range(100) if x % 10 == 0]
Copyright © 2013 Russel Winder 22
x = {x * x for x in range(100) if x % 10 == 0}
Copyright © 2013 Russel Winder 23
x = {x: x * x for x in range(100) if x % 10 == 0}
Copyright © 2013 Russel Winder 24
Sadly (!) Python 2.7 is getting
backports of things from Python 3.
Copyright © 2013 Russel Winder 25
Fortunately Python 2.6
and earlier are not.
Copyright © 2013 Russel Winder 26
Python 2 is strict,
Python 3 is (sort of) lazy
Copyright © 2013 Russel Winder 27
map(lambda x: x * x, filter(lambda x: x % 10 == 0, range(100))
Copyright © 2013 Russel Winder 28
range(x)
xrange(x)
tuple(range(x))
range(x)→
Copyright © 2013 Russel Winder 29
Not Sequences, but Iterators
items
keys
values
iteritems
iterkeys
itervalues
items
keys
values
→
Copyright © 2013 Russel Winder 30
Competition
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
Copyright © 2013 Russel Winder 32
Native code languages are now
as expressive as Python.
Copyright © 2013 Russel Winder 33
And native code programs are
way, way faster than Python programs.
Copyright © 2013 Russel Winder 34
Statically compiled languages catch errors at
compile time that are horrendous run
time bugs in dynamic languages.
Copyright © 2013 Russel Winder 35
Are you concurrent?
● Who's using thread?● Who's using threading?
Copyright © 2013 Russel Winder 36
Are you parallel?
● Who's using thread?● Who's using threading?● Who's using multiprocessing?
Copyright © 2013 Russel Winder 37
At least IronPython and Jython
do not have a GIL as
CPython and PyPy do.
Copyright © 2013 Russel Winder 38
Support the PyPy STM/AME experiment.
Copyright © 2011–2012 Russel Winder 39
NB CSP stands for
Concurrent Sequential Processes
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.
Copyright © 2011–2012 Russel Winder 41
ActorsIndependent processes communicating via asynchronous exchange of messages
Copyright © 2011–2012 Russel Winder 42
DataflowOperators connected by channels with activity triggered by arrival of data on the channels.
Copyright © 2011–2012 Russel Winder 43
CSPSequential processes connected by channels using synchronous message exchange (rendezvous).
Copyright © 2013 Russel Winder 44
Data ParallelismTransform a sequence to another sequence where all individual actions happen at the same time.
Copyright © 2013 Russel Winder 45
Futures are the future.
Copyright © 2013 Russel Winder 46
Along with process pools
and thread pools.
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.
Copyright © 2013 Russel Winder 50
Conclusion
Copyright © 2013 Russel Winder 51
There are no real reasons
to stay with Python 2…
Copyright © 2013 Russel Winder 52
…so Python 3 should be
Considered mandatory.
Copyright © 2013 Russel Winder 53
Challenge
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?
Copyright © 2013 Russel Winder 55
Interstitial Advertisement
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