4 Keys to Writing Modern Python

Learn how to write modern Python code that takes advantage of the language's newest and most powerful features, including type hinting, Python virtual environments and package management, new Python syntax, and Python testing.
4 Keys to Writing Modern Python

4 Keys to Writing Modern Python

Writing modern Python code that takes advantage of the language’s newest and most powerful features requires exploring four key areas: type hinting, Python virtual environments and package management, new Python syntax, and Python testing.

Type Hinting in Python

Python’s recently introduced type hinting syntax 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. Each successive revision of Python rolls out more sophisticated and powerful type annotations.

“Type hints are optional, not mandatory. Not every project needs them. Use type hints to make your bigger projects comprehensible, but feel free to omit them from a 50-line throwaway script.”

Type hinting in Python

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. However, recent advances in Python’s tooling give you more options: Pyenv, Pipenv, Poetry, PDM, Hatch, and uv.

Python package management

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.

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. Structural pattern matching lets you make control-flow decisions based on the contents or structure of objects.

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.

New Python syntax

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.