I quite often find myself directly assigning a class' attributes from values passed to the __init__:
class SomeThing:
def __init__(self, a, b, c, d, e ...):
self.a = a
self.b = b
self.c = c
...
Is there some way to streamline this?
Dataclasses are designed especially for this kind of __init__ functions.
With them you can write your class like this:
@dataclass
class SomeThing:
a: int
b: str
c: float
d: bool
e: list
# __init__ will be generated automatically based on fields declaration above
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With