Python is a dynamic language in more ways than one: Not only is it not a static language like C or C++, but it’s also constantly evolving. If you want to get up to speed on what happened in the world of Python in March 2021, then you’ve come to the right place to get your news!
March 2021 marks a notable change to the core of the Python language with the addition of structural pattern matching, which is available for testing now in the latest alpha release of Python 3.10.0.
Beyond changes to the language itself, March was a month full of exciting and historical moments for Python. The language celebrated its 30th birthday and became one of the first open-source technologies to land on another planet.
Let’s dive into the biggest Python news from the past month!
Free Bonus: 5 Thoughts On Python Mastery, a free course for Python developers that shows you the roadmap and the mindset you’ll need to take your Python skills to the next level.
Python Turns 30 Years Old
Although Python’s actual birth date is February 20, 1991, which is when version 0.9.0 was released, March is a good month to celebrate. This March is the 20th anniversary of the Python Software Foundation, which was founded on March 6, 2001.
In its thirty years, Python has changed—a lot—both as a language and as an organization. The transition from Python 2 to Python 3 took a decade to complete. The organizational model for decision-making changed too: The creator of the language, Guido van Rossum, used to be at the helm, but a five-person steering council was created in 2018 to plan the future of Python.
Happy birthday, Python! Here’s to many more years 🥂
Structural Pattern Matching Comes to Python 3.10.0
Python 3.10.0 is the next minor version of Python and is expected to drop on October 4, 2021. This update will bring a big addition to the core syntax: structural pattern matching, which was proposed in PEP 634. You could say that structural pattern matching adds a sort of switch
statement to Python, but that isn’t entirely accurate. Pattern matching does much more.
For instance, take an example from PEP 635. Suppose you need to check if an object x
is a tuple containing host and port information for a socket connection and, optionally, a mode such as HTTP or HTTPS. You could write something like this using an if
…elif
…else
block:
if isinstance(x, tuple) and len(x) == 2:
host, port = x
mode = "http"
elif isinstance(x, tuple) and len(x) == 3:
host, port, mode = x
else:
# Etc…
Python’s new structural pattern matching allows you to write this more cleanly using a match
statement:
match x:
case host, port:
mode = "http"
case host, port, mode:
pass
# Etc…
match
statements check that the shape of the object matches one of the cases and binds data from the object to variable names in the case
expression.
Not everyone is thrilled about pattern matching, and the feature has received criticism from both within the core development team and the wider community. In the acceptance announcement, the steering council acknowledged these concerns while also expressing their support for the proposal:
We acknowledge that Pattern Matching is an extensive change to Python and that reaching consensus across the entire community is nearly impossible. Different people have reservations or concerns around different aspects of the semantics and the syntax (as does the Steering Council). In spite of this, after much deliberation, … we are confident that Pattern Matching as specified in PEP 634, et al, will be a great addition to the Python Language. (Source)
Although opinions are divided, pattern matching is coming to the next Python release. You can learn more about how pattern matching works by reading the tutorial in PEP 636.
Python Lands on Mars
On February 18, the Perseverance Mars rover landed on Mars after a seven-month journey. (Technically, this is a February news item, but it’s so cool that we have to include it this month!)
Perseverance brought a wide range of new instruments and scientific experiments that will give scientists their best look at Mars yet. Perseverance relies on a host of open-source software and off-the-shelf hardware, making it the most accessible Mars rover project to date.
Python is one of the open-source technologies living on Perseverance. It was used on-board the rover to process images and video taken during landing.
One of the most exciting experiments carried by Perseverance is the Ingenuity Mars helicopter, which is a small drone being used to test flight in the thin Martian atmosphere. Python is one of the development requirements for the flight control software, which is called F’.
2020 Python Developers Survey Results Are In
The results from the 2020 Python Developers Survey conducted by JetBrains and the Python Software Foundation are in, and they show some interesting changes compared to last year’s survey.
In 2020, 94% of respondents report primarily using Python 3, which is up from 90% in 2019 and 75% in 2017. Interestingly, Python 2 still sees use among a majority of respondents in the computer graphics and game development segments.
Flask and Django continue to dominate web frameworks with 46% and 43% adoption, respectively. The newcomer FastAPI was the third most popular web framework at 12% adoption—an incredible feat considering 2020 was the first year the framework appeared in the list of options.
Visual Studio Code gained 5% more of the share of responses to the question “What is the main editor you use for your current Python development?” That puts Microsoft’s IDE at 29% of the share and further closes the gap between Visual Studio Code and PyCharm, which still tops the list at 33%.
Check out the survey results to see even more stats about Python and its ecosystem.
New Features Coming to Django 3.2
Django 3.2 will be released sometime in April 2021, and with it comes an impressive list of new features.
One major update adds support for functional indexes, which allow you to index expressions and database functions, such as indexing lowercased text or a mathematical formula involving one or more database columns.
Functional indexes are created in the Meta.indexes
option in a Model
class. Here’s an example adapted from the official release notes:
from django.db import models
from django.db.models import F, Index, Value
class MyModel(models.Model):
height = models.IntegerField()
weight = models.IntegerField()
class Meta:
indexes = [
Index(
F("height") / (F("weight") + Value(5)),
name="calc_idx",
),
]
This creates a functional index called calc_idx
that indexes an expression that divides the height
field by the weight
field and then adds 5
.
Support for PostgreSQL covering indexes is another index-related change coming in Django 3.2. A covering index lets you store multiple columns in a single index. This allows queries containing only the index fields to be satisfied without an additional table lookup. In other words, your queries can be much faster!
Another notable change is the addition of Admin site decorators that streamline the creation of custom display and action functions.
For a complete list of new features coming in Django 3.2, check out the official release notes. Real Python contributor Haki Benita also has a helpful overview article that walks you through some of the upcoming features with more context and several examples.
PEP 621 Reaches Final Status
Way back in 2016, PEP 518 introduced the pyproject.toml
file as a standardized place to specify a project’s build requirements. Previously, you could specify metadata only in a setup.py
file. This caused some problems because executing setup.py
and reading the build dependencies requires installing some of the build dependencies.
pyproject.toml
has gained popularity in the last few years and is now being used for much more than just storing build requirements. Projects like the black
autoformatter use pyproject.toml
to store package configuration.
PEP 621, which was provisionally accepted in November 2020 and marked final on March 1, 2021, specifies how to write a project’s core metadata in a pyproject.toml
file. On the surface, this might seem like a less significant PEP, but it represents a continued movement away from the setup.py
file and points to improvements in Python’s packaging ecosystem.
PyPI Is Now a GitHub Secret Scanning Integrator
The Python Package Index, or PyPI, is the place to download all of the packages that make up Python’s rich ecosystem. Between the pypi.org
website and files.pythonhosted.org
, PyPI generates over twenty petabytes of traffic a month. That’s over 20,000 terabytes!
With so many people and organizations relying on PyPI, keeping the index secure is paramount. This month, PyPI became an official GitHub secret scanning integrator. GitHub will now check every commit to public repositories for leaked PyPI API tokens and will disable repositories and notify their owners if any are found.
What’s Next for Python?
Python continues to grow with increasing momentum. As more users turn to the language for more and more tasks, it’s only natural that Python and its ecosystem will continue to evolve. At Real Python, we’re excited about Python’s future and can’t wait to see what new things are in store for us in April.
What’s your favorite piece of Python news from March? Did we miss anything notable? Let us know in the comments, and we might feature you in next month’s Python news roundup.
Happy Pythoning!