Skip to content

Instantly share code, notes, and snippets.

@gavinwahl
Created April 1, 2015 21:44
Show Gist options
  • Save gavinwahl/ef4be04fadf0296f005f to your computer and use it in GitHub Desktop.
Save gavinwahl/ef4be04fadf0296f005f to your computer and use it in GitHub Desktop.
Inheritance and composition are isomorphic
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
@gavinwahl
Copy link
Author

There is a mechanical transformation from Child1 to Child2 and vice-versa.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment