Welcome, guest | Sign In | My Account | Store | Cart

Module mixins allow you to inherit from modules. This is how ruby achieves multiple inheritance, and I just wanted to show it's possible in python as well.

Python, 2 lines
1
2
def mixin(mod):
    return type("mixin(%s)" % (mod.__name__,), (object,), mod.__dict__)

see http://sebulba.wikispaces.com/recipe+module+mixins for more info and examples.

3 comments

Ori Peleg 17 years, 7 months ago  # | flag

Cool. What about accessing 'self'?

Ori Peleg 17 years, 7 months ago  # | flag

Okay, saw it in the wiki page.

bearophile - 17 years, 7 months ago  # | flag

Maybe the mixin function can do the include too, so this:

import testmixin

class Bar(mixin(testmixin)):

Can be replaced with this:

class Bar(mixin("testmixin")):

Created by tomer filiba on Tue, 29 Aug 2006 (PSF)
Python recipes (4591)
tomer filiba's recipes (12)

Required Modules

  • (none specified)

Other Information and Tasks