1 of 33

Python 3 and Your Next Project

by Nick Sloan

2 of 33

Python 3.0 - The Highlights

  • Print is a function
  • Views and iterators instead of lists
  • Ordering comparisons require natural ordering
  • long renamed to int
  • 1/2 returns float
  • Text vs Data replaces Unicode vs 8-bit

3 of 33

Python 3.0 - The Highlights

  • Annotations
  • Keyword-only args
  • Keyword args for class definitions
  • nonlocal statement
  • Extended iterable unpacking
  • Dictionary comps
  • Set literals
  • New octal literals
  • New binary literals
  • Bytes literals
  • Raise expr from expr

4 of 33

Python 3.0 - The Highlights

  • New string formatting
  • super() without args
  • No more unbound methods

5 of 33

Features - Unicode*

Strings are unicode by default, and a lot has changed:

>>> text = 'This is some text.'

>>> print(text)

This is some text.

>>> print(text.encode('utf_8'))

b'This is some text.'

6 of 33

Features - Unicode*

This is cool:

>>> 名前 = 'ニック'

>>> re.findall(r'\w', 名前)

['ニ', 'ッ', 'ク']

7 of 33

Features - Print Function*

Print is now a function with more options:

print(‘This text’, ‘is printed.’,

sep=’,’, end=’\n’,

file=”sys.stdout”, flush=True)

8 of 33

Features - New String Formatting*

Python 3 has new string formatting:

“Hello {0}. You are {state}.”.format(

‘Nick’, state=”awesome”

)

9 of 33

Features - Sensible Ordering

You can no longer compare types that don’t make sense:

>>> 'yes' > 3

TypeError: unorderable types: str() > int()

10 of 33

Features - Annotations

The annotations syntax has no semantics:

def my_function(

a:’Whatever’,

password:int=0

) -> ‘return annotation’:

pass

11 of 33

Features - Keyword-Only Arguments

Keyword-only arguments can be specified after the varargs to make them keyword-only:

def my_function(a, b, *args, kwa=None):

The varargs operator can appear by itself to indicate no additional positional arguments:

def my_function(*, kwarga=None):

12 of 33

Features - Unpacking Catchall

New catchall for unpacking:

>>> a, b, *c, d = range(5)

>>> a, b, d

(0, 1, 4)

>>> c

[2, 3]

13 of 33

Features - New Literals

{1, 2, 3, 4} # Set

b’hello world.’ # Bytes

0o720 # Octal

0b100101 # Binary

14 of 33

Features - Exception Chaining

It’s trivial to chain exceptions together now:

try:

except Exception as e:

raise MyException from e

15 of 33

Features - Float Division Default

Dividing two integers used to yield an integer:

1 / 2 == 0

As of Python 3, it now yields a float:

1 / 2 == 0.5

However, floor (integer) division is still available:

1 // 2 == 0

16 of 33

Features - Argument-less super()

The super() function no longer requires arguments when it is called within a class definition.

class B(A):

def __init__(self):

super().__init__()

17 of 33

Python 3.1 and 3.2 - The Highlights

  • Ordered dicts
  • Multiple context managers
  • Dict-based config for logging
  • .pyc repo directories
  • WSGI 1.0.1
  • argparse
  • concurrent.futures

18 of 33

Features - Multiple With Statements*

Nesting with statements has just gotten easier:

with open(file) as f, DB.conn() as sess:

# do some stuff with files and

# databases

19 of 33

Python 3.3 - The Highlights

  • Generator delegation
  • Return of u’’ str syntax
  • Reworked I/O Exception Hierarchy
  • Rewritten import machinery
  • More compact unicode strings and attribute dicts
  • faulthandler
  • ipaddress
  • lzma
  • unittest.mock
  • venv

20 of 33

Features - Generator Delegation

Delegate the work of a generator to another generator:

def g(x):

yield ‘Start’

yield from range(x)

yield ‘End’

21 of 33

Features - Included Virtualenvs

Virtual environments for everyone!

$ pyvenv /path/to/myvenv

$ source /path/to/myvenv/bin/activate

22 of 33

Python 3.4 - The Highlights

  • Better codec handling
  • Better Import API
  • Single-dispatch generic functions
  • Pickle Protocol v4
  • TLSv1.1 TLSv1.2 support for SSL
  • asyncio
  • ensurepip
  • enum
  • pathlib
  • selectors
  • statistics
  • tracemalloc

23 of 33

Features - asyncio

There are a lot of asynchronous networking libraries for Python.

The core team finally bundled one. Check it out because it looks cool.

24 of 33

Features - pip is Bundled

Python 3.4 and later will ship with pip, and Python’s venv module will bootstrap its virtual environments with pip by default.

25 of 33

Python 3 Adoption

Of the top 200 Python packages on PyPI, only 55 do not support Python 3, and only 21 are in the top 100:

Boto, Paste, Paramiko, MySQL-Python, Fabric, PasteScript, Supervisor, Carbon, Graphite, Aspen, Meld3, Gevent, Suds, Twisted, Python-Novaclient, SQLObject, Eventlet, Minitage.Paste, Deliverance, OAuth2, Python-Cloudfiles

26 of 33

Python 3 Adoption

Of those 21, four are applications that can be run in a separate virtual environment from the rest of your code:

Supervisor, Carbon, Graphite, and Deliverance

27 of 33

Python 3 Adoption

Of those 21, two are officially deprecated or end-of-lifed:

Python-Cloudfiles, Minitage.Paste

28 of 33

Python 3 Adoption

Of those 21, six have not been updated in over a year, and four of those have been neglected for two years or more:

Deliverance, Meld3, Paste, PasteScript, Suds, OAuth2

29 of 33

Python 3 Adoption

Of those 21, one has a really stupid name:

OAuth2 is a sequel to the OAuth library. It supports the OAuth spec, and

does not support the OAuth2 spec.

30 of 33

Python 3 Adoption

Of those 21, Ian Bicking is the author of four:

Paste, PasteScript, SQLObject, and Deliverance

31 of 33

Python 3 Adoption

Boto is a popular package for interfacing with Amazon Web Services. Low level support for Python 3.x is available now in the boto-core package. boto 3 is coming soon, but will represent a significant departure from the 2.x series.

32 of 33

Python 3 Adoption

Though not in the top 100 packages, it should be noted that NLTK released a third alpha that supports Python 3 in November.

33 of 33

Python 3 Adoption

The following popular packages support

Python 3:

Django, Flask, SciPy, Jinja2, Requests,