Writing Modern Python: 4 Essential Concepts to Master
Python has come a long way since its inception in 1991. With its 30-year anniversary in 2021, the language has continued to evolve, and its adoption has grown exponentially. As a result, many features of Python have remained unchanged, but with every new edition, there are new ways of doing things and new libraries that take advantage of those advances.
Python programming
In this article, we’ll explore four key concepts to help you write modern Python code that takes advantage of the language’s latest and greatest features.
Type Hinting in Python
Type hinting is a crucial aspect of modern Python development. It allows linters and third-party code quality tools to analyze your code before runtime, detecting possible errors before they occur. The more you create Python code to share with others, the more likely you (and everyone else!) will benefit from using type hints.
Type hinting
Each successive revision of Python rolls out more sophisticated and powerful type annotations. If you get into the habit of learning how to use type annotations in the short run, you will be better equipped to make use of each new type hinting innovation as it’s introduced.
Python Virtual Environments and Package Management
For simple projects and undemanding development jobs, you can often just use Python’s built-in venv
tool to keep projects and their requirements separate. But recent advances in Python’s tooling give you more options:
- Pyenv: If you need to keep multiple versions of Python installed to satisfy different project requirements, Pyenv lets you switch between them either globally or on a per-project basis.
- Pipenv: Billed as “Python dev workflow for humans,” Pipenv is meant to manage a virtual environment plus all the dependencies for your project.
- Poetry: Expanding on Pipenv’s toolset, Poetry not only manages projects and requirements but also makes it easy to deploy the project to PyPI.
- PDM: (short for Python Development Master) is a recent cutting-edge project in this vein.
- Hatch: The hatch project not only handles project setup and management but also provides a build system, tools for packaging projects for redistribution on PyPI, test handling, and many other useful functions.
- uv: The experimental uv project is written by the same folks who make the ruff Python linting tool.
Package management
When creating new projects that are meant to be worked on in a team environment or distributed to others (e.g., via PyPI), be sure to use the modern pyproject.toml
format for your requirements and project configuration, along with the project layout used with it.
New Python Syntax
Python’s evolution has meant many new additions to the language itself. The last few versions of Python have added useful syntactical constructions that allow for more powerful and succinct programming. While they aren’t mandatory, newer third-party modules may use them, so they’re worth getting to know at least casually.
New Python syntax
Three recent syntax additions are especially notable:
- Pattern matching: The biggest recent addition, structural pattern matching, which arrived in Python 3.10, is more than just “switch/case for Python” as it has sometimes been described.
- The ‘walrus operator’: So named for its appearance (:=), the walrus operator, added in Python 3.8, introduces assignment expressions, a way to assign a value to a variable and then apply a test to the variable in a single step.
- Positional-only parameters: A minor but useful recent addition to Python’s syntax, positional-only parameters lets you indicate which function parameters must be specified as positional ones, never as keyword arguments.
Python Testing
Writing tests for a codebase is like flossing daily: Everyone agrees it’s a good thing, few of us actually do it, and even fewer do it properly. Modern Python codebases deserve to have test suites, and the current tooling for testing makes creating test suites easier than ever.
Python testing
Python has its own built-in testing framework, unittest
. It isn’t bad as a default, but its design and behaviors are dated. The Pytest framework has risen to prominence as a common substitute. It’s more flexible (you can declare tests in any part of your code, not just a subset) and requires writing far less boilerplate.