I need to base the decision of which classes to derive from based on a flag.
For instance:
if SOME_FLAG:
class A(B,C):
...definitions...
else:
class A(B):
...definitions...
The question is how to avoid duplicating the ...definitions...
I tried using argument lists like one would do for normal functions, but it does not work for classes:
base_classes = [B,C] if SOME_FLAG else [B]
class A(*base_classes):
...definitions...
But this syntax is invalid.
(I know there are tricks for dynamically adding superclasses, but this case is different: it's a definition-time customization, not a full-blown dynamic redefinition of base classes)
The easiest way is probably to conditionally define an intermediate class, then derive class
A
from that.It's also possible to write a metaclass that accepts a sequence for the parent classes, though that seems like overkill: