Most people know the issue with mutable defaults in Python. But what’s the best way to fix it?
The issue class User: def __init__(self, name: str, emails: list[str] = []) -> None: self.name = name self.emails = emails def add_email(self, email: str) -> None: self.emails.append(email) james = User(name="James") james.add_email("james@gmail.com") john = User(name="John") # John will have the emails ['james@gmail.com'], even though we never added that email to John's list. # That's a bug!