Created
August 21, 2012 12:57
-
-
Save janpipek/3415140 to your computer and use it in GitHub Desktop.
Inheritance and constructors 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
#!/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