2021-08-07 00:51:22 +02:00
|
|
|
"""
|
|
|
|
diamond1.py: Demo of diamond-shaped class graph.
|
2021-02-15 00:58:46 +01:00
|
|
|
|
2021-08-07 00:51:22 +02:00
|
|
|
# tag::LEAF_MRO[]
|
|
|
|
>>> Leaf.__mro__ # doctest:+NORMALIZE_WHITESPACE
|
|
|
|
(<class 'diamond1.Leaf'>, <class 'diamond1.A'>, <class 'diamond1.B'>,
|
|
|
|
<class 'diamond1.Root'>, <class 'object'>)
|
2021-02-15 00:58:46 +01:00
|
|
|
|
2021-08-07 00:51:22 +02:00
|
|
|
# end::LEAF_MRO[]
|
|
|
|
|
|
|
|
# tag::DIAMOND_CALLS[]
|
|
|
|
>>> leaf1 = Leaf() # <1>
|
|
|
|
>>> leaf1.ping() # <2>
|
|
|
|
<instance of Leaf>.ping() in Leaf
|
|
|
|
<instance of Leaf>.ping() in A
|
|
|
|
<instance of Leaf>.ping() in B
|
|
|
|
<instance of Leaf>.ping() in Root
|
2021-02-15 00:58:46 +01:00
|
|
|
|
2021-08-07 00:51:22 +02:00
|
|
|
>>> leaf1.pong() # <3>
|
|
|
|
<instance of Leaf>.pong() in A
|
|
|
|
<instance of Leaf>.pong() in B
|
|
|
|
|
|
|
|
# end::DIAMOND_CALLS[]
|
|
|
|
"""
|
|
|
|
|
|
|
|
# tag::DIAMOND_CLASSES[]
|
|
|
|
class Root: # <1>
|
|
|
|
def ping(self):
|
|
|
|
print(f'{self}.ping() in Root')
|
2021-02-15 00:58:46 +01:00
|
|
|
|
|
|
|
def pong(self):
|
2021-08-07 00:51:22 +02:00
|
|
|
print(f'{self}.pong() in Root')
|
2021-02-15 00:58:46 +01:00
|
|
|
|
2021-08-07 00:51:22 +02:00
|
|
|
def __repr__(self):
|
|
|
|
cls_name = type(self).__name__
|
|
|
|
return f'<instance of {cls_name}>'
|
2021-02-15 00:58:46 +01:00
|
|
|
|
|
|
|
|
2021-08-07 00:51:22 +02:00
|
|
|
class A(Root): # <2>
|
2021-02-15 00:58:46 +01:00
|
|
|
def ping(self):
|
2021-08-07 00:51:22 +02:00
|
|
|
print(f'{self}.ping() in A')
|
2021-02-15 00:58:46 +01:00
|
|
|
super().ping()
|
|
|
|
|
2021-08-07 00:51:22 +02:00
|
|
|
def pong(self):
|
|
|
|
print(f'{self}.pong() in A')
|
2021-02-15 00:58:46 +01:00
|
|
|
super().pong()
|
2021-08-07 00:51:22 +02:00
|
|
|
|
|
|
|
|
|
|
|
class B(Root): # <3>
|
|
|
|
def ping(self):
|
|
|
|
print(f'{self}.ping() in B')
|
|
|
|
super().ping()
|
|
|
|
|
|
|
|
def pong(self):
|
|
|
|
print(f'{self}.pong() in B')
|
|
|
|
|
|
|
|
|
|
|
|
class Leaf(A, B): # <4>
|
|
|
|
def ping(self):
|
|
|
|
print(f'{self}.ping() in Leaf')
|
|
|
|
super().ping()
|
|
|
|
# end::DIAMOND_CLASSES[]
|