Question: Is there a reasonable way in Python to implement mixin behavior similar to that found in Ruby -- that is, without using inheritance?

```
class Mixin(object):
    def b(self): print "b()"
    def c(self): print "c()"

class Foo(object):
    # Somehow mix in the behavior of the Mixin class,
    # so that all of the methods below will run and
    # the issubclass() test will be False.

    def a(self): print "a()"

f = Foo()
f.a()
f.b()
f.c()
print issubclass(Foo, Mixin)

```

I had a vague idea to do this with a class decorator, but my attempts led to confusion. Most of my searches on the topic have led in the direction of using inheritance (or in more complex scenarios, multiple inheritance) to achieve mixin behavior.

Answer: 
```
def mixer(*args):
    """Decorator for mixing mixins"""
    def inner(cls):
        for a,k in ((a,k) for a in args for k,v in vars(a).items() if callable(v)):
            setattr(cls, k, getattr(a, k).im_func)
        return cls
    return inner

class Mixin(object):
    def b(self): print "b()"
    def c(self): print "c()"

class Mixin2(object):
    def d(self): print "d()"
    def e(self): print "e()"

@mixer(Mixin, Mixin2)
class Foo(object):
    # Somehow mix in the behavior of the Mixin class,
    # so that all of the methods below will run and
    # the issubclass() test will be False.

    def a(self): print "a()"

f = Foo()
f.a()
f.b()
f.c()
f.d()
f.e()
print issubclass(Foo, Mixin)

```

output:

```
a()
b()
c()
d()
e()
False

```
I am not that familiar with Python, but from what I know about Python metaprogramming, you could actually do it pretty much the same way it is done in Ruby.

In Ruby, a module basically consists of two things: a pointer to a method dictionary and a pointer to a constant dictionary. A class consists of three things: a pointer to a method dictionary, a pointer to a constant dictionary and a pointer to the superclass.

When you mix in a module `M` into a class `C`, the following happens:

1. an anonymous class `α` is created (this is called an *include class*)
2. `α`'s method dictionary and constant dictionary pointers are set equal to `M`'s
3. `α`'s superclass pointer is set equal to `C`'s
4. `C`'s superclass pointer is set to `α`

In other words: a fake class which shares its behavior with the mixin is injected into the inheritance hierarchy. So, Ruby actually *does* use inheritance for mixin composition.

I left out a couple of subleties above: first off, the module doesn't actually get inserted as `C`'s superclass, it gets inserted as `C`'s superclasses' (which is `C`'s singleton class) superclass. And secondly, if the mixin itself has mixed in other mixins, then *those* also get wrapped into fake classes which get inserted directly above `α`, and this process is applied recursively, in case the mixed in mixins in turn have mixins.

Basically, the whole mixin hierarchy gets flattened into a straight line and spliced into the inheritance chain.

AFAIK, Python actually allows you to change a class's superclass(es) after the fact (something which Ruby does *not* allow you to do), and it also gives you access to a class's `dict` (again, something that is impossible in Ruby), so you should be able to implement this yourself.