Skip to content

Instantly share code, notes, and snippets.

@theodoregoetz
Last active November 19, 2015 16:08
Show Gist options
  • Save theodoregoetz/5cd738606cd830bbdf41 to your computer and use it in GitHub Desktop.
Save theodoregoetz/5cd738606cd830bbdf41 to your computer and use it in GitHub Desktop.
Diamond Pattern in Python 3
print(r'''
The Classic Diamond Pattern
or
How I Learned to Stop Worrying and Love Python
--> := 'inherets from'
A
^
/ \
/ \
B <-- C
^ ^
\ /
\ /
D
''')
class A(object):
def __init__(self,a):
print(' A.init')
self.a = a
def fn(self):
print(' A.fn',self.a)
class B(A):
def __init__(self,a,b):
print(' B.init')
super().__init__(a)
self.b = b
def fn(self):
print(' B.fn',self.a,self.b)
super().fn()
class C(B,A):
def __init__(self,a,b,c):
print(' C.init')
super().__init__(a,b)
self.c = c
def fn(self):
print(' C.fn',self.a,self.b,self.c)
super().fn()
class D(C,B):
def __init__(self,a,b,c,d):
print(' D.init')
super().__init__(a,b,c)
self.d = d
def fn(self):
print(' D.fn',self.a,self.b,self.c,self.d)
super().fn()
print('A(1)')
a = A(1)
print('B(1,2)')
b = B(1,2)
print('C(1,2,3)')
c = C(1,2,3)
print('D(1,2,3,4)')
d = D(1,2,3,4)
print('a.fn()')
a.fn()
print('b.fn()')
b.fn()
print('c.fn()')
c.fn()
print('d.fn()')
d.fn()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment