Protocol

say it: “PROH-tuh-kawl” /ˈproʊtəkɔːl/

Describes what an object must be able to do, whatever class it is.

What is a Protocol in Python typing?

A Protocol describes what an object must be able to do, like "has a pay() method", without caring which class it comes from.

from typing import Protocol

class PaymentMethod(Protocol):
    def pay(self, amount: float) -> None: ...

def checkout(method: PaymentMethod, total: float) -> None:
    method.pay(total)   # any object with pay() works

Level: Freelancer · More Python terms at this level

Updated

In other subjects