Python 3 and Your Next Project
by Nick Sloan
Python 3.0 - The Highlights
Python 3.0 - The Highlights
Python 3.0 - The Highlights
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.'
Features - Unicode*
This is cool:
>>> 名前 = 'ニック'
>>> re.findall(r'\w', 名前)
['ニ', 'ッ', 'ク']
Features - Print Function*
Print is now a function with more options:
print(‘This text’, ‘is printed.’,
sep=’,’, end=’\n’,
file=”sys.stdout”, flush=True)
Features - New String Formatting*
Python 3 has new string formatting:
“Hello {0}. You are {state}.”.format(
‘Nick’, state=”awesome”
)
Features - Sensible Ordering
You can no longer compare types that don’t make sense:
>>> 'yes' > 3
TypeError: unorderable types: str() > int()
Features - Annotations
The annotations syntax has no semantics:
def my_function(
a:’Whatever’,
password:int=0
) -> ‘return annotation’:
pass
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):
Features - Unpacking Catchall
New catchall for unpacking:
>>> a, b, *c, d = range(5)
>>> a, b, d
(0, 1, 4)
>>> c
[2, 3]
Features - New Literals
{1, 2, 3, 4} # Set
b’hello world.’ # Bytes
0o720 # Octal
0b100101 # Binary
Features - Exception Chaining
It’s trivial to chain exceptions together now:
try:
…
except Exception as e:
raise MyException from e
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
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__()
Python 3.1 and 3.2 - The Highlights
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
Python 3.3 - The Highlights
Features - Generator Delegation
Delegate the work of a generator to another generator:
def g(x):
yield ‘Start’
yield from range(x)
yield ‘End’
Features - Included Virtualenvs
Virtual environments for everyone!
$ pyvenv /path/to/myvenv
$ source /path/to/myvenv/bin/activate
Python 3.4 - The Highlights
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.
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.
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
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
Python 3 Adoption
Of those 21, two are officially deprecated or end-of-lifed:
Python-Cloudfiles, Minitage.Paste
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
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.
Python 3 Adoption
Of those 21, Ian Bicking is the author of four:
Paste, PasteScript, SQLObject, and Deliverance
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.
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.
Python 3 Adoption
The following popular packages support
Python 3:
Django, Flask, SciPy, Jinja2, Requests,