Python introduced Protocol in PEP 544 (Python Enhancement Proposal) and officially added it in Python 3.8. If you’re coming from a Golang background and diving into Python, you might stumble upon Python’s Protocol and think, “Wait a minute… isn’t this just Golang’s interface{}?” And honestly? You’re not entirely wrong! But let’s break it down and see if they’re really long-lost brothers or just distant cousins.
What is Protocol in Python?
The idea is simple: give Python a way to define interfaces without forcing inheritance.
Here’s a simple example of how it works:
from typing import Protocol
class Flyer(Protocol):
def fly(self) -> None:
...
class Bird:
def fly(self) -> None:
print("Flapping wings!")
class Airplane:
def fly(self) -> None:
print("Engaging turbines!")
def take_off(entity: Flyer):
entity.fly()
take_off(Bird()) # Flapping wings!
take_off(Airplane()) # Engaging turbines!
Key takeaway: Any class with a fly method can be treated as a Flyer, even if it doesn’t explicitly say, “Hey, I implement Flyer!” That’s duck typing at its finest!
How Does This Compare to Golang’s interface{}?
If you’ve ever coded in Go before, you know that interfaces define behavior, not structure. Just like Python’s Protocol, Go’s interfaces allow a type to be considered valid if it implements the expected methods.
Here’s a Go version of the same example:
type Flyer interface {
Fly()
}
type Bird struct{}
func (b Bird) Fly() {
fmt.Println("Flapping wings!")
}
type Airplane struct{}
func (a Airplane) Fly() {
fmt.Println("Engaging turbines!")
}
func TakeOff(f Flyer) {
f.Fly()
}
TakeOff(Bird{}) // Flapping wings!
TakeOff(Airplane{}) // Engaging turbines!
Looks super similar, right? In both cases:
- There’s no need for explicit inheritance or
implementskeywords. - If an object has the right methods, it qualifies as the interface/protocol.
- This promotes duck typing: “If it quacks like a duck, it’s a duck.”
Who Inspired Whom? 🤔
At first glance, it might seem like Python copied Golang. After all, Go has had interfaces since day one (back in 2009), whereas Python only got Protocol in 2019. But hold on! Duck typing has been a core concept in Python since forever. What Protocol really did was give duck typing a more structured, type-safe way of working with tools like mypy. So, while Go’s interface might have influenced Python’s Protocol, Python’s dynamic nature already had the foundation for this idea.
My Take?
Neither is copying the other. Instead, I think they both solve the same problem in a way that makes sense for their language:
- Go’s
interface{}is a fundamental part of the language, making it possible to write generic, loosely coupled code in a statically typed world. - Python’s
Protocolis more of an optional tool for better static type checking while keeping the language’s flexible, dynamic nature.
When Should/Shouldn’t We Use Protocol in Python?
Alright, so when does Protocol actually make sense in Python?
✅ When you want static type safety – If you’re using mypy, Protocol helps ensure that objects conform to the expected behavior before runtime.
✅ When you’re writing libraries – If you’re designing an API that others will use, Protocol lets you define expected behaviors without forcing users to subclass.
✅ When working with multiple implementations – Just like in the Flyer example, if you have multiple objects with the same behavior but different implementations, Protocol keeps things clean.
❌ When working with simple, dynamic Python code – If your project doesn’t need static type checks, regular duck typing (just using .fly()) is more Pythonic and readable.
So, are Python’s Protocol and Golang’s interface{} sibling? They’re definitely from the same family of ideas. They both encourage behavior-based programming, for making code more flexible and modular.
But while Go was designed with interfaces as a core feature from the start, Python’s Protocol is more of a nice-to-have for those who need type safety while still enjoying Python’s dynamic nature.
What do you think? Have you used both Protocol and Go’s interface{}? Let’s discuss in the comments!
Refrences:
- Fluent Python 2nd edition, Chapter 13: Interfaces, Protocols, and ABCs
- The Go Programming Language, Chapter 7: Interfaces
Python’s type hints will eventually become mandatory?