Are you wondering why you should switch from Python 3.6 to Python 3.7 or any other version combination? Or are you thinking about supporting Python 3.5 and want to know which features you have to avoid? Then this is your article.
There are, of course, way more features. But those are the ones I stumble over most of the time.
pyenv
Before we start going into details, you must know about pyenv. It’s a super handy tool which lets you easily switch the Python version. It downloads them automatically for you. Here is how you use it:
# See which Python versions are available:
$ pyenv install --list
# Install one
$ pyenv install 3.5.9
# Use one:
$ pyenv local 3.5.9
It’s pretty neat that it also switches the tools you install with it. For example, when you switch to 3.5.9, you also have pip for that version.
Python 3.5
I really hope nobody is using Python versions below 3.3 anymore. Or, god forbid, even Python 2.7 😨.
There are so many new features and improvements in Python 3 compared to 2.7:
# python2 thinks this is fine, python3 throws an Exception
>>> True = False
>>> max(['foo', 2])
'foo'
>>> 'foo' > 2
True
# Advanced iterable unpacking:
a, b, *rest = range(10)
And there is more:
- Better unicode handling 🎉🎉🎉
- Range is the new xrange (the old range function was dropped)
- PEP-380:
yield frominstead of iterating over a generator andyield-ing every value - enum, pathlib and unittest.mock
- functools.lru_cache
- async and await (source)
Please also be aware that Python 2.7, Python 3.1 / 3.2 / 3.3 and 3.4 reached their end of life a while ago. And Python 3.5 will reach it in September 2020 (source).
Python 3.6
# Reasonable type annotation syntax
>>> from typing import List
>>> number: List[int] = [28, 4, 1990]
# f-string!
>>> bar = 3
>>> f"foo {bar}"
'foo 3'
# PEP 515: Underscores in Numeric Literals
>>> number = 1_000_000
The list of new features in Python 3.6 is way longer. One improvement to point out is that the internal representation of dictionaries became way more efficient, leading to a 20% size reduction in memory.
Python 3.7
The future annotations are super handy when you want to properly annotate your classes.
from __future__ import annotations
class Foo:
def __init__(self, bar):
self.bar = bar
def foo(self, bar) -> Foo:
self.bar = bar + bar
return self
- Dictionaries have insertion order (source)
- async and await are reserved keywords (source)
- PEP-557: Dataclasses
Python 3.8
# f-strings for debugging
>>> bar = 3
>>> foo = "Hello World"
>>> f"{foo=}, {bar=}"
"foo='Hello World', bar=3"
# Assignment expressions (aka: The Walrus Operator)
>>> a = 6
>>> if is_positive := a > 0:
... print("It's positive!")
... else:
... print("It's negative")
...
It's positive!
TypedDict is also pretty cool (example taken from PEP-589):
from typing import TypedDict
class Movie(TypedDict):
name: str
year: int
movie: Movie = {"name": "Blade Runner", "year": 1982}
Python 3.9
Dictionary Union:
>>> a = {'foo': 'bar', 42: 1337}
>>> b = {'x': 'y', 'a': 'b', 'c': 'd', 'foo': 'foo'}
>>> a | b
{'foo': 'foo', 42: 1337, 'x': 'y', 'a': 'b', 'c': 'd'}
>>> b | a
{'x': 'y', 'a': 'b', 'c': 'd', 'foo': 'bar', 42: 1337}
Python 3.10
PEP 619 is the release schedule of Python 3.10, which was released in October 2021.
We got better error messages (PEP 626) which is awesome! We also got improvements for type annotations 🎉
The elephant in the room is structural pattern matching (PEP 634, PEP 635, PEP 636). The following example is taken from PEP 636:
# Before structural pattern matching
command = input("What are you doing next? ")
command_parts = command.split()
if len(command_parts) == 1:
if command_parts[0] == "quit":
print("Goodbye!")
quit_game()
elif command_parts[0] == "look":
current_room.describe()
elif len(command_parts) == 2:
if command_parts[0] == "get":
obj = command_parts[1]
character.get(obj, current_room)
elif command_parts[0] == "go":
direction = command_parts[1]
current_room = current_room.neighbor(direction)
# With structural pattern matching
command = input("What are you doing next? ")
match command.split():
case ["quit"]:
print("Goodbye!")
quit_game()
case ["look"]:
current_room.describe()
case ["get", obj]:
character.get(obj, current_room)
case ["go", direction]:
current_room = current_room.neighbor(direction)
Python 3.11
- PEP 673: The
Selftype 😍
Python 3.12
PEP 693 contains the release schedule and the What’s New page shows:
- PEP 701: f-strings can now contain the quotes they were created with.
Python 3.13
PEP 719 contains the release schedule and the What’s New page shows:
- A new interactive interpreter with multi-line editing and colored output
- PEP 703: An experimental free-threaded build without the GIL
- PEP 744: An experimental JIT compiler
Python 3.14
PEP 745 contains the release schedule and the What’s New page shows: