-
-
Save sashaegorov/82f4e7386c02c5d533b56ef76d38d1f9 to your computer and use it in GitHub Desktop.
Understanding super and multiple inheritance in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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