How to apply default value to Python dataclass field when None was passed?

The simple solution is to just implement the default arguments in __post_init__() only!

@dataclass
class Specs2:
    a: str
    b: str
    c: str

    def __post_init__(self):
        if self.b is None:
            self.b = 'Bravo'
        if self.c is None:
            self.c="Charlie"

(Code is not tested. If I got some detail wrong, it wouldn’t be the first time)

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)