Skip to content

Commit b656170

Browse files
committed
Sample code for the article on Python protocols
1 parent c0b2da3 commit b656170

21 files changed

+491
-0
lines changed

python-protocol/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python Protocols: Leveraging Structural Subtyping
2+
3+
This folder provides the code examples for the Real Python tutorial [Python Protocols: Leveraging Structural Subtyping](https://realpython.com/python-protocol/).

python-protocol/adder_v1.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from typing import Protocol
2+
3+
4+
class Adder(Protocol):
5+
def add(self, x, y):
6+
...
7+
8+
9+
class IntAdder:
10+
def add(self, x, y):
11+
return x + y
12+
13+
14+
class FloatAdder:
15+
def add(self, x, y):
16+
return x + y
17+
18+
19+
def add(adder: Adder) -> None:
20+
print(adder.add(2, 3))
21+
22+
23+
add(IntAdder())
24+
add(FloatAdder())
25+
26+
27+
for adder in [IntAdder(), FloatAdder()]:
28+
add(adder)

python-protocol/adder_v2.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Protocol
2+
3+
4+
class Adder(Protocol):
5+
def add(self, x: float, y: float) -> float:
6+
...
7+
8+
9+
class IntAdder:
10+
def add(self, x: int, y: int) -> int:
11+
return x + y
12+
13+
14+
def add(adder: Adder) -> None:
15+
print(adder.add(2, 3))
16+
17+
18+
# add(IntAdder())

python-protocol/adder_v3.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from typing import Protocol
2+
3+
4+
class Adder(Protocol):
5+
def add(self, x: int | float, y: int | float) -> int | float:
6+
...
7+
8+
9+
class IntAdder:
10+
def add(self, x: int, y: int) -> int:
11+
return x + y
12+
13+
14+
def add(adder: Adder) -> None:
15+
print(adder.add(2, 3))
16+
17+
18+
# add(IntAdder())

python-protocol/adder_v4.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from typing import Protocol, TypeVar
2+
3+
T = TypeVar("T", bound=int | float | str)
4+
5+
6+
class Adder(Protocol[T]):
7+
def add(self, x: T, y: T) -> T:
8+
...
9+
10+
11+
class IntAdder:
12+
def add(self, x: int, y: int) -> int:
13+
return x + y
14+
15+
16+
class FloatAdder:
17+
def add(self, x: float, y: float) -> float:
18+
return x + y
19+
20+
21+
class StrAdder:
22+
def add(self, x: str, y: str) -> str:
23+
return x + y
24+
25+
26+
def add(adder: Adder) -> None:
27+
print(adder.add(2, 3))
28+
29+
30+
add(IntAdder())
31+
add(FloatAdder())
32+
add(StrAdder())

python-protocol/adder_v5.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Protocol
2+
3+
4+
class Adder(Protocol):
5+
def add[T: int | float | str](self, x: T, y: T) -> T:
6+
...
7+
8+
9+
class IntAdder:
10+
def add(self, x: int, y: int) -> int:
11+
return x + y
12+
13+
14+
class FloatAdder:
15+
def add(self, x: float, y: float) -> float:
16+
return x + y
17+
18+
19+
class StrAdder:
20+
def add(self, x: str, y: str) -> str:
21+
return x + y
22+
23+
24+
def add(adder: Adder) -> None:
25+
print(adder.add(2, 3))
26+
27+
28+
add(IntAdder())
29+
add(FloatAdder())
30+
add(StrAdder())

python-protocol/adder_v6.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from typing import Protocol, TypeVar
2+
3+
4+
class Adder(Protocol):
5+
def add[T: int | float | str](self, x: T, y: T) -> T:
6+
...
7+
8+
9+
class IntAdder:
10+
def add(self, x: int, y: int) -> int:
11+
return x + y
12+
13+
14+
class FloatAdder:
15+
def add(self, x: float, y: float) -> float:
16+
return x + y
17+
18+
19+
class StrAdder:
20+
def add(self, x: str, y: str) -> str:
21+
return x + y
22+
23+
24+
def add(adder: Adder) -> None:
25+
print(adder.add(2, 3))
26+
27+
28+
add(IntAdder())
29+
add(FloatAdder())
30+
add(StrAdder())

python-protocol/animals_v1.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Animal:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
def eat(self):
6+
print(f"{self.name} is eating.")
7+
8+
def drink(self):
9+
print(f"{self.name} is drinking.")
10+
11+
12+
class Dog(Animal):
13+
def bark(self):
14+
print(f"{self.name} is barking.")
15+
16+
17+
class Cat(Animal):
18+
def meow(self):
19+
print(f"{self.name} is meowing.")

python-protocol/animals_v2.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
class Dog:
2+
def __init__(self, name):
3+
self.name = name
4+
5+
def eat(self):
6+
print(f"{self.name} is eating.")
7+
8+
def drink(self):
9+
print(f"{self.name} is drinking.")
10+
11+
def make_sound(self):
12+
print(f"{self.name} is barking.")
13+
14+
15+
class Cat:
16+
def __init__(self, name):
17+
self.name = name
18+
19+
def eat(self):
20+
print(f"{self.name} is eating.")
21+
22+
def drink(self):
23+
print(f"{self.name} is drinking.")
24+
25+
def make_sound(self):
26+
print(f"{self.name} is meowing.")

python-protocol/birds_v1.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Duck:
2+
def quack(self):
3+
return "The duck is quacking!"
4+
5+
6+
def make_it_quack(duck: Duck) -> str:
7+
return duck.quack()
8+
9+
10+
class Person:
11+
def quack(self):
12+
return "The person is imitating a duck quacking!"
13+
14+
15+
print(make_it_quack(Duck()))
16+
# print(make_it_quack(Person()))

0 commit comments

Comments
 (0)