Skip to content

Instantly share code, notes, and snippets.

@mbylstra
Last active September 20, 2016 06:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbylstra/aae6eac98d162a5cc9c2 to your computer and use it in GitHub Desktop.
Save mbylstra/aae6eac98d162a5cc9c2 to your computer and use it in GitHub Desktop.
python mixin inheritance
#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# MixinParent
# |
# Mixin A
# └--┐ |
# B
class A(object):
def test(self):
# super(A, self).test() #if put in, AttributeError: 'super' object has no attribute 'test'
print 'A'
class MixinParent(object):
def test(self):
super(MixinParent, self).test() #if left out, A's test() method will not be called.
print 'MixinParent'
class Mixin(MixinParent):
def test(self):
super(Mixin, self).test()
print 'Mixin'
class B(Mixin, A):
def test(self):
super(B, self).test()
print 'B'
b = B()
b.test()
# output:
# > A
# > MixinParent
# > Mixin
# > B
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment