-1

I am wanting to make a function that creates a new class from a variable amount of so-called mixin classes. My first instinct is to use *args ...

>>> def mixins(*args):
        class Foo(*args):

SyntaxError: invalid syntax

... but didn't get very far ... Since that didn't work, I tried this, which seems to work:

>>> def mixins(*args):
        class Foo(args[0]):
            pass
        for arg in args[1:]:
            class Foo(Foo, arg):
                pass
        return Foo

Question

Are there other approaches to solving this problem?

Motivation

I have created an Abstract Base Class that has many abstract methods. I have several types of subclasses, which each implement some of the needed abstract methods but not all of them. Sometimes I can create a working instance by mixing in 2 subclasses, sometimes it takes more to implement all of the abstract methods.

Wesley Baugh
  • 3,720
  • 4
  • 24
  • 42
  • My instinct tells me there must be a better approach. Can you explain exactly what the motivation of doing that? Perhaps using class-decorators would be more suitable. Does your problem require an inheritance-based solution? – shx2 Mar 12 '13 at 06:10
  • @shx2 I have added text about the motivation for the question. – Wesley Baugh Mar 12 '13 at 06:15

2 Answers2

2

You can do this using the type function:

def mixins(*args):
    return type('Foo', args, {})

You might also want to make sure that the class always inherits from object:

def mixins(*args):
    bases = args if object in args else args + (object,)
    return type('Foo', bases, {})
grc
  • 22,885
  • 5
  • 42
  • 63
  • Excellent! I remember reading about the `type` function recently in this excellent (and long) answer for [What is a metaclass in Python?](http://stackoverflow.com/a/6581949/1988505). – Wesley Baugh Mar 12 '13 at 06:21
1

Yes. Why not?

Here is how you can make class objects on the fly:

type('a',(object,),{})

This will create a class object of type - type and which derives from the object base class and whose attributes are an empty dictionary.

So, in your case,the call would be:

type('Foo',args,{})

Here, args will be contain all the base classes you want to inherit Foo from. and the resultant would be a clas object which you can use to return from mixins.

GodMan
  • 2,561
  • 2
  • 24
  • 40