Skip to content

Instantly share code, notes, and snippets.

@litzomatic
Created August 1, 2012 17:39
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save litzomatic/3229116 to your computer and use it in GitHub Desktop.
Save litzomatic/3229116 to your computer and use it in GitHub Desktop.
Understanding super and multiple inheritance in Python.
class Bar(object):
def __init__(self, bar='bar', *args, **kwargs):
self.bar = bar
super(Bar, self).__init__(*args, **kwargs)
class Foo(object):
def __init__(self, foo='foo', *args, **kwargs):
self.foo = foo
super(Foo, self).__init__(*args, **kwargs)
class Baz(Foo, Bar):
def __init__(self, baz='baz', *args, **kwargs):
self.baz = baz
super(Baz, self).__init__(*args, **kwargs)
baz = Baz()
vars(baz)
Out[154]: {'bar': 'bar', 'baz': 'baz', 'foo': 'foo'}
mad_baz = Baz(baz='BAZ!!!', bar='BAR!!!', foo='FOO!!!')
vars(mad_baz)
Out[156]: {'bar': 'BAR!!!', 'baz': 'BAZ!!!', 'foo': 'FOO!!!'}
pos_baz = Baz('baz_pos', 'foo_pos', 'bar_pos')
vars(pos_baz)
Out[158]: {'bar': 'bar_pos', 'baz': 'baz_pos', 'foo': 'foo_pos'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment