I'd like to have a class that adds in mixins based on arguments passed to the constructor. This is what I've tried:
class MixinOne(object):
def print_name(self):
print("{} is using MixinOne.".format(self.name))
class MixinTwo(object):
def print_name(self):
print("{} is using MixinTwo.".format(self.name))
class Sub(object):
def __new__(cls, *args, **kwargs):
mixin = args[1]
if mixin == 'one':
bases = (MixinOne,) + cls.__bases__
elif mixin == 'two':
bases = (MixinTwo,) + cls.__bases__
return object.__new__(type('Sub', bases, dict(cls.__dict__)))
def __init__(self, name, mixin):
print('In Sub.__init__')
self.name = name
The only problem with this seems to be that __init__
doesn't get called, so the print_name
methods will not work.
- How do I get
__init__
onSub
to fire?
or
- Is there a better way to do this?