Skip to content

Instantly share code, notes, and snippets.

@janpipek
Created August 21, 2012 12:57
Show Gist options
  • Save janpipek/3415140 to your computer and use it in GitHub Desktop.
Save janpipek/3415140 to your computer and use it in GitHub Desktop.
Inheritance and constructors in Python
#!/bin/env/python
# How inheritance of constructors in python works.
# Only simple cases without multiple inheritance are covered.
class A(object):
"""No inheritance"""
def __init__(self):
print "A"
class B(A):
"""Constructor overriden, parent is lost"""
def __init__(self):
print "B"
class C(A):
"""No constructor in child, parent is preserved"""
pass
class D(A):
"""Constructor overriden, parent explicitly called"""
def __init__(self):
super(D,self).__init__()
print "D"
A()
print "---"
B()
print "---"
C()
print "---"
D()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment