Python's dynamic nature and support for duck typing have long been praised for their flexibility. However, as codebases grow larger and more complex, the benefits of static type checking become increasingly apparent. But how can we reconcile the flexibility of duck typing with the safety of static type checking? Enter Python's Protocol class.
In this tutorial, you'll learn:
Duck typing is a programming concept where the type or class of an object is less important than the methods it defines. It's based on the idea that "If it looks like a duck, swims like a duck, and quacks like a duck, then it probably is a duck."
In Python, duck typing is fully supported. For example:
class Duck: def quack(self): print("Quack!") class Person: def quack(self): print("I'm imitating a duck!") def make_it_quack(thing): # Note: No type hint here thing.quack() duck = Duck() person = Person() make_it_quack(duck) # Output: Quack! make_it_quack(person) # Output: I'm imitating a duck!
In this example, make_it_quack doesn't care about the type of thing. It only cares that thing has a quack method. Note that there's no type hint for the thing parameter, which is typical in duck-typed code but can lead to issues in larger codebases.
Duck typing offers several advantages:
However, it also has some drawbacks:
One approach to addressing these issues is using Abstract Base Classes (ABCs). Here's an example:
from abc import ABC, abstractmethod class Quacker(ABC): @abstractmethod def quack(self): pass class Duck(Quacker): def quack(self): print("Quack!") class Person(Quacker): def quack(self): print("I'm imitating a duck!") def make_it_quack(thing: Quacker): thing.quack() duck = Duck() person = Person() make_it_quack(duck) make_it_quack(person)
While this approach provides better type checking and clearer interfaces, it has disadvantages:
Python 3.8 introduced the Protocol class, which allows us to define interfaces without requiring inheritance. Here's how we can use it:
from typing import Protocol class Quacker(Protocol): def quack(self):... class Duck: def quack(self): print("Quack!") class Person: def quack(self): print("I'm imitating a duck!") def make_it_quack(thing: Quacker): thing.quack() duck = Duck() person = Person() make_it_quack(duck) make_it_quack(person)
Let's break this down:
This approach gives us several benefits:
Here's a more complex example showing how Protocols can be as complex as needed (Shape), keeping your domain classes (Circle, Rectangle) flat:
from typing import Protocol, List class Drawable(Protocol): def draw(self): ... class Resizable(Protocol): def resize(self, factor: float): ... class Shape(Drawable, Resizable, Protocol): pass def process_shapes(shapes: List[Shape]): for shape in shapes: shape.draw() shape.resize(2.0) # Example usage class Circle: def draw(self): print("Drawing a circle") def resize(self, factor: float): print(f"Resizing circle by factor {factor}") class Rectangle: def draw(self): print("Drawing a rectangle") def resize(self, factor: float): print(f"Resizing rectangle by factor {factor}") # This works with any class that has draw and resize methods, # regardless of its actual type or inheritance shapes: List[Shape] = [Circle(), Rectangle()] process_shapes(shapes)
In this example, Circle and Rectangle don't inherit from Shape or any other class. They simply implement the required methods (draw and resize). The process_shapes function can work with any objects that have these methods, thanks to the Shape protocol.
Protocols in Python provide a powerful way to bring static typing to duck-typed code. They allow us to specify interfaces in the type system without requiring inheritance, maintaining the flexibility of duck typing while adding the benefits of static type checking,
By using Protocols, you can:
If you want to learn more about Protocols and type hinting in Python, check out the official Python documentation on the typing module, or explore advanced static type checking tools like mypy.
Happy coding, and may your ducks always quack with type safety!
You can find more of my content, including my newsletter here
The above is the detailed content of Duck Typing Meets Type Hints: Using Protocols in Python. For more information, please follow other related articles on the PHP Chinese website!