Skip to content

Instantly share code, notes, and snippets.

@motivic
Created August 25, 2016 21:20
Show Gist options
  • Save motivic/ee035d1bf589526727c5b0a9b2a8a063 to your computer and use it in GitHub Desktop.
Save motivic/ee035d1bf589526727c5b0a9b2a8a063 to your computer and use it in GitHub Desktop.
Using super in Python 2.7
# http://stackoverflow.com/questions/222877/how-to-use-super-in-python
class SomeBaseClass(object):
def __init__(self):
print('SomeBaseClass.__init__(self) called')
class ChildClass(SomeBaseClass):
def __init__(self):
print('ChildClass.__init__(self) called')
SomeBaseClass.__init__(self)
class SuperChildClass(SomeBaseClass):
def __init__(self):
print('SuperChildClass.__init__(self) called')
super(SuperChildClass, self).__init__()
class InjectMe(SomeBaseClass):
def __init__(self):
print('InjectMe.__init__(self) called')
super(InjectMe, self).__init__()
# behavior depends on inheritance order
class UnsuperInjector(InjectMe, ChildClass): pass
# class UnsuperInjector(ChildClass, InjectMe): pass
class SuperInjector(InjectMe, SuperChildClass): pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment