I'm creating a Python class that will use its parameters to define two other classes within this class. For example, ClassA
contains ClassB
and ClassC
. Because of this, ClassA has a lot of parameters.
I'm currently solving this using **kwargs
for classB
and classC
arguments, and saving only the input parameters for classA
parameters. For example:
class A:
def __init__(self, a1, a2, a3, **kwargs):
self.a1 = a1
self.a2 = a2
self.a3 = a3
# Get B and C kwargs
default_params = {
'b1': kwargs.get('b1', 1),
'b2': kwargs.get('b1', 2),
'c1': kwargs.get('c1', 1),
'c2': kwargs.get('c1', 2),
}
for param in default_params:
self.__dict__[param] = default_params[param]
B = classB(a1=self.a1, b1=self.b1, b2=self.b2)
C = classC(a2=self.a2, c1=self.c1, c2=self.c2)
Is this a worse solution compared to the below?
class A:
def __init__(
self,
a1, a2, a3,
b1, b2, c1, c2):
self.a1 = a1
self.a2 = a2
self.a3 = a3
self.b1 = b1
self.b2 = b2
self.c1 = c1
self.c2 = c2
My concern is if arguments for class B and C are super numerous, then class A's attributes seems too many. On the other hand, the first approach seems to take up more lines anyway.
Another way is if I pass only kwargs to classB
and classC
, but this will have an error when there are some parameters not present in one of the classes.
Is there a pythonic way I'm failing to see here for classes with many attributes and utilising this kind of design pattern?
Aucun commentaire:
Enregistrer un commentaire