Move to Python 3!

Graham Markall - @gmarkall

Ian Ozsvald - @ianozsvald, ianozsvald.com



https://github.com/gmarkall/py3lightning

Py3 Improvements over Py2

An (incomplete) list:

Brief demos of:

Matrix Multiplication - @ operator

_images/matexpr.png

Old "syntax":

M.dot(M.T).dot(N.dot(v))

New syntax:

M @ M.T @ N @ v

Defining matrix multiplication for your own classes:

def __matmul__(self, other)  # @
def __rmatmul__(self, other) # @
def __imatmul__(self, other) # @=

Advanced Unpacking

Get "everything else" from an iterable:

>>> a, b, *rest = range(10)
>>> a
0
>>> b
1
>>> rest
[2, 3, 4, 5, 6, 7, 8, 9]

Get the first and last lines of a file:

>>> with open('using_python_to_profit.txt') as f:
        first, *_, last = f.readlines()
>>> first
'Step 1: Use Python 3\n'
>>> last
'Step 10: Profit!\n'

Keyword-only Args

def cleanup(folder, *, extreme=False):
    if extreme:
        shutil.rmtree('/')
    else:
        shutil.rmtree(folder)
>>> cleanup('/home/gmarkall/tmp', '/home/gmarkall/tmp2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: cleanup() takes 1 positional argument but 2 were given
_images/drmkwargs.png

Compatibility with common libraries

_images/py3liblogos.png

Py3-only libraries

_images/whymovenow.png

“Ubuntu 15.10 (Wily Werewolf) to Switch to Python 3.5 Ahead of Ubuntu 16.04 LTS”

How to upgrade