protocol

The term protocol became overloaded with two subtly different meanings. The first meaning refers to internal protocols, such as the iterator, context manager, and descriptor protocols.

These protocols are widely understood in the community and consist of magic or special methods that make up a given protocol. For example, the .__iter__() and .__next__() methods define the iterator protocol.

Python 3.8 introduced a second, slightly different type of protocol. These protocols specify the methods and attributes that a class must implement to be considered of a given type.

This type of protocols are part of Python’s type hint system and are defined in the typing module.

Tutorial

Python Protocols: Leveraging Structural Subtyping

In this tutorial, you'll learn about Python's protocols and how they can help you get the most out of using Python's type hint system and static type checkers.

intermediate python

For additional information on related topics, take a look at the following resources:


By Dan Bader • Updated Feb. 14, 2025