Protocols

The terms "iterator protocol" or "descriptor protocol" are already familiar and have been used for a long time. However, now protocols can be described in the form of code and checked for compliance during static analysis.

A protocol is described as a regular class inheriting from Protocol. It can have methods (including those with implementations) and fields. Actual classes implementing the protocol can inherit from it, but this is not mandatory, as shown in the example.

Although they are not exactly interfaces, they are still a cool feature. It’s also important to note that protocols, like all other type annotation features, are mainly used with the statically typed mypy.

Comments (0)

Leave a comment