Commit a37e68
2026-08-05 17:41:42 Niklas Polke: Create Dataclass| /dev/null .. python/dataclass.md | |
| @@ 0,0 1,27 @@ | |
| + | # Dataclass |
| + | A **dataclass** is a Python feature that automatically generates common methods (such as `__init__`,` __repr__`, and `__eq__`) for classes that primarily store data. It reduces boilerplate code and makes data models easier to read and maintain. |
| + | |
| + | ## Overview |
| + | ```python |
| + | from dataclasses import dataclass |
| + | |
| + | @dataclass(slots=True, frozen=True) |
| + | class search_element: |
| + | ... |
| + | ``` |
| + | |
| + | ## `slots=True` |
| + | Using slots=True creates a fixed set of attributes for each instance instead of a dynamic `__dict__`. |
| + | |
| + | Benefits: |
| + | - Reduces memory usage. |
| + | - Improves attribute access performance. |
| + | - Prevents adding new attributes at runtime. |
| + | |
| + | ## `frozen=True` |
| + | Using frozen=True makes instances effectively immutable after creation. |
| + | |
| + | Benefits: |
| + | - Prevents accidental modification of object state. |
| + | - Makes objects safer to share across code. |
| + | - Allows instances to be hashable (when appropriate), so they can be used as dictionary keys or stored in sets. |
