Master Python's object model: the __eq__/__hash__ contract, the NotImplemented value, context managers and structural typing with Protocol.
Open this lesson in KodokonIn Python, every operator, every conversion, every with statement goes through a special method: a == b calls a.__eq__(b), len(x) calls x.__len__(). Be careful to distinguish __repr__ - the unambiguous representation aimed at the developer, shown in the debugger and inside lists - from __str__, aimed at the end user. If __str__ is missing, Python falls back to __repr__: so always implement __repr__ first, ideally in a form that lets you reconstruct the object.
class Money:
def __init__(self, amount: int, currency: str):
self.amount = amount
self.currency = currency
def __repr__(self) -> str:
return f"Money({self.amount!r}, {self.currency!r})"
def __eq__(self, other: object) -> bool:
if not isinstance(other, Money):
return NotImplemented
return (self.amount, self.currency) == (
other.amount, other.currency)
print(Money(10, "EUR") == Money(10, "EUR"))
print(Money(10, "EUR") == "10 EUR")Returning NotImplemented (the value, not to be confused with the NotImplementedError exception) triggers the operator negotiation protocol: Python then tries the reflected operation other.__eq__(self), and only concludes False as a last resort. Returning False directly would short-circuit that negotiation and break comparisons with subclasses or third-party types that would in fact know how to respond.
import time
class Timer:
def __enter__(self):
self.start = time.perf_counter()
return self
def __exit__(self, exc_type, exc, tb):
stop = time.perf_counter()
self.elapsed = stop - self.start
return False
with Timer() as timer:
sum(range(1_000_000))
print(f"{timer.elapsed:.4f} s")If __exit__ returns True, the current exception is suppressed - that's exactly how contextlib.suppress works. To avoid writing a full class, @contextlib.contextmanager turns a generator into a context manager: the code before yield plays the role of __enter__, the code after (ideally in a finally) that of __exit__. Another pillar of the object model: the Protocol types from the typing module formalize duck typing - a class is compatible if it exposes the right methods, with no declared inheritance.
from typing import Protocol, runtime_checkable
@runtime_checkable
class Closable(Protocol):
def close(self) -> None: ...
class Session:
def close(self) -> None:
print("session closed")
def shutdown(resource: Closable) -> None:
resource.close()
shutdown(Session())
print(isinstance(Session(), Closable))__eq__, what should you return when facing a type you cannot compare against?__eq__ to a class without touching __hash__. What is the consequence?__exit__ method returning True mean?