Reputation: 2964
I have the following issue with class inheritance. Given class A
, B
, C
as follows
class A(object):
...
class B(A):
...
class C(A):
...
I want a class D
that can either inherit from B
or from C
, depending on the use case.
Right now, I have solved this issue by having a dynamic class definition:
def D(base_class, parameter1, parameter2):
class D(base_class):
...
return D(parameter1, parameter2)
Is this the proper way to do it, or is there a better way of solving this issue?
Upvotes: 0
Views: 600
Reputation: 531798
Rather than have D
both create a class and return an instance, have it just return the class, which you can then use to create multiple instances as necessary.
def make_D(base_class):
class D(base_class):
...
return D
DB = make_D(B)
DC = make_D(C)
d1 = DB(...)
d2 = DC(...)
d3 = DC(...)
At this point, you should consider whether you actually need a factory function to define your subclasses, rather than simply define DB
and DC
directly.
Upvotes: 1