I need a class that will accept a number of parameters, I know that all parameters will be provided but some maybe passed as None in which case my class will have to provide default values.
I want to setup a simple dataclass with a some default values like so:
@dataclass
class Specs1:
a: str
b: str = 'Bravo'
c: str = 'Charlie'
I would like to be able to get the default value for the second field but still set a value for the third one. I cannot do this with None because it is happily accepted as a value for my string:
r1 = Specs1('Apple', None, 'Cherry') # Specs1(a='Apple', b=None, c='Cherry')
I have come up with the following solution:
@dataclass
class Specs2:
def_b: ClassVar = 'Bravo'
def_c: ClassVar = 'Charlie'
a: str
b: str = def_b
c: str = def_c
def __post_init__(self):
self.b = self.def_b if self.b is None else self.b
self.c = self.def_c if self.c is None else self.c
Which seems to behave as intended:
r2 = Specs2('Apple', None, 'Cherry') # Specs2(a='Apple', b='Bravo', c='Cherry')
However, I feel it is quite ugly and that I am maybe missing something here. My actual class will have more fields so it will only get uglier.
The parameters passed to the class contain None and I do not have control over this aspect.

所有评论(0)