I've written a mixin class that's designed to be layered on top of a new-style class, for example via
class MixedClass(MixinClass, BaseClass):
pass
What's the smoothest way to apply this mixin to an old-style class? It is using a call to super
in its __init__
method, so this will presumably (?) have to change, but otherwise I'd like to make as few changes as possible to MixinClass
. I should be able to derive a subclass that makes the necessary changes.
I'm considering using a class decorator on top of a class derived from BaseClass
, e.g.
@old_style_mix(MixinOldSchoolRemix)
class MixedWithOldStyleClass(OldStyleClass)
where MixinOldSchoolRemix
is derived from MixinClass
and just re-implements methods that use super
to instead use a class variable that contains the class it is mixed with, in this case OldStyleClass
. This class variable would be set by old_style_mix
as part of the mixing process.
old_style_mix
would just update the class dictionary of e.g. MixedWithOldStyleClass
with the contents of the mixin class (e.g. MixinOldSchoolRemix
) dictionary.
Is this a reasonable strategy? Is there a better way? It seems like this would be a common problem, given that there are numerous available modules still using old-style classes.