Skip to content

Instantly share code, notes, and snippets.

@shearichard
Created April 30, 2013 11:41
Show Gist options
  • Save shearichard/5488183 to your computer and use it in GitHub Desktop.
Save shearichard/5488183 to your computer and use it in GitHub Desktop.
Demonstration of how mixins can be used to consolidate common __init__ functionality for classes that have differing superclasses
class SomeMixin(object):
def __init__(self, rel=None, attrs=None, *args, **kwargs):
print 'Testing sharing an __init__ via a mixin class'
self.someattr = "sm"
super(SomeMixin, self).__init__(*args, **kwargs)
class SomeSuper1(object):
def __init__(self, rel=None, attrs=None, *args, **kwargs):
print 'Testing sharing an __init__ via a super1 class'
class SomeSuper2(object):
def __init__(self, rel=None, attrs=None, *args, **kwargs):
print 'Testing sharing an __init__ via a super2 class'
class TestClass1(SomeMixin, SomeSuper1):
def __init__(self, rel=None, attrs=None, *args, **kwargs):
super(TestClass1, self).__init__(*args, **kwargs)
self.banana = "tc1"
print ("base1")
class TestClass2(SomeMixin, SomeSuper2):
def __init__(self, rel=None, attrs=None, *args, **kwargs):
super(TestClass2, self).__init__(*args, **kwargs)
self.banana = "tc2"
print ("base2")
print "About to instantiate TestClass1"
t1 = TestClass1()
print "Finished instantiate of TestClass1"
print "About to instantiate TestClass2"
t2 = TestClass2()
print "Finished instantiate of TestClass2"
print t2.someattr
print t2.banana
print t1.someattr
print t1.banana
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment