Created
April 1, 2015 21:44
-
-
Save gavinwahl/ef4be04fadf0296f005f to your computer and use it in GitHub Desktop.
Inheritance and composition are isomorphic
This file contains hidden or 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 Parent(object): | |
def __init__(self, x): | |
self.x = x | |
def frob(self): | |
self.x += 1 | |
class Child1(Parent): | |
def __init__(self, x, y): | |
self.y = y | |
super(Child1, self).__init__(x) | |
def frob(self): | |
self.y += 1 | |
super(Child1, self).frob() | |
class Child2(object): | |
def __init__(self, x, y): | |
self.y = y | |
self.parent = Parent(x) | |
def frob(self): | |
self.y += 1 | |
self.parent.frob() | |
@property | |
def x(self): | |
return self.parent.x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There is a mechanical transformation from Child1 to Child2 and vice-versa.