What to read #2
List of links of interest materials to read with short description.
Code is run more than read
Don't forget about priorities in software development.
user > ops > dev
biz > ops > dev
biz ≹ user
Use object
instead of Any
In Python typing, Any
disables checks. If you don't care about type of variable (for example passing *args, **kwargs
to super().method(*args, **kwargs)
and do not use them youself), use object
from example import Widget
class BlueWidget(Widget):
def __init__(self, *args: object, blueness: int = 50, **kwargs: object) -> None:
super().__init__(*args, **kwargs)
self.blueness = blueness
Python errors as values
Throwing exceptions as error handling is implicit for caller. Use union types with exception as function result type to explicitly define errors the function can return.
# Define a function that returns a union of a User and an error
def get_user(user_id: str) -> User | Exception:
rows = users.find(user_id=user_id)
if len(rows) == 0:
return Exception("user not found")
return rows[0]
def rename_user(user_id: str, name: str) -> User | Exception:
# Consume the function
user = get_user(user_id)
if isinstance(user, Exception):
return user
user.name = name
return user