In Python, protocols refer to a concept introduced in Python 3.8 as a way to define structural typing or "duck typing" within the language. A protocol is a set of methods or attributes that an object must have in order to be considered compatible with that protocol. Protocols enable you to define interfaces without explicitly creating a class or inheriting from a specific base class.
Protocols are defined using the typing.Protocol
class or the typing.Protocol
decorator. Let's explore the details with examples:
Defining a Protocol:
To define a protocol, you can use the typing.Protocol
class or the typing.Protocol
decorator. Here's an example using the class syntax:
from typing import Protocol
class Printable(Protocol):
def print(self) -> None:
pass
In this example, we define a protocol named Printable
that requires an object to have a print
method. The Protocol
class allows you to define abstract methods (in this case, just print
) that must be implemented by objects conforming to the protocol.
Implementing a Protocol:
To implement a protocol, you don't need to explicitly declare it. Instead, you can ensure that an object conforms to a protocol by implementing the required methods. Here's an example:
class Book:
def __init__(self, title: str):
self.title = title
def print(self) -> None:
print(f"Book Title: {self.title}")
In this example, the Book
class implements the Printable
protocol by providing the required print
method. Any instance of the Book
class can be treated as a Printable
object.
Using a Protocol:
Protocols are primarily used for type hinting and static type checking. You can use a protocol as a type annotation to indicate that an argument or variable must conform to a specific protocol. Here's an example:
def print_object(obj: Printable) -> None:
obj.print()
In this example, the print_object
function takes an argument obj
of type Printable
. This means that any object passed to this function must implement the print
method defined in the Printable
protocol.
Multiple Protocols:
You can also define a protocol that combines multiple other protocols. Here's an example:
class Serializable(Protocol):
def serialize(self) -> str:
pass
class PrintableAndSerializable(Printable, Serializable):
pass
In this example, we define a protocol named Serializable
with a serialize
method. Then we define another protocol named PrintableAndSerializable
that combines both the Printable
and Serializable
protocols.
To conclude with,Protocols provide a way to define structural typing in Python, allowing you to create interfaces without the need for explicit inheritance. They enable type checkers to verify that objects adhere to the defined protocols, enhancing code correctness and maintainability.
Top comments (0)