Python Dataclasses: Eliminating Boilerplate Without Shared-State Bugs
Use @dataclass to generate __init__ and __repr__ automatically, but mutable defaults, frozen instances, and inheritance ordering introduce subtle bugs if you skip the mechanics.
ReadMeFeed / Community knowledge
Real questions. Useful conversations. Find the people who know your stack.
Use @dataclass to generate __init__ and __repr__ automatically, but mutable defaults, frozen instances, and inheritance ordering introduce subtle bugs if you skip the mechanics.
Use @dataclass with __post_init__ to validate inputs and compute derived fields after initialization. Learn how to enforce invariants, handle mutable defaults, work with frozen dataclasses, and avoid common pitfalls like shared mutable state and inheritance issues.
Now that Python 3.10 has been released, is there any preference when indicating that a parameter or returned value might be optional, i.e., can be None . So what is preferred: Option 1: def f(parameter: Optional[int]) -> Optional[str]: Option 2: def f(parameter: int | None) -> str | None: Also, is there any preference between Type | None and None | Typ
I'm trying to understand the new structural pattern matching syntax in Python 3.10. I understand that it is possible to match on literal values like this: def handle(retcode): match retcode: case 200: print('success') case 404: print('not found') case _: print('unknown') handle(404) # not found However, if I refactor and move these values to module-level var
I am trying to use multiple cases in a function similar to the one shown below so that I can be able to execute multiple cases using match cases in python 3.10 def sayHi(name): match name: case ['Egide', 'Eric']: return f"Hi Mr {name}" case 'Egidia': return f"Hi Ms {name}" print(sayHi('Egide')) This is just returning None instead of the message, even if I re
Asked · 07 Apr 2025, 03:20 UTC
commented · 11 Apr 2025, 22:32 UTC
answered · 16 Apr 2022, 19:44 UTC