forked from fluentpython/example-code-2e
-
Notifications
You must be signed in to change notification settings - Fork 0
/
diamond2.py
67 lines (49 loc) · 1.68 KB
/
diamond2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"""
unrelated.py: examples with ``super()`` in a sibling class.
``U`` is unrelated (does not subclass ``Root``)
Calling ``ping`` on an instance of ``U`` fails::
# tag::UNRELATED_DEMO_1[]
>>> u = U()
>>> u.ping()
Traceback (most recent call last):
...
AttributeError: 'super' object has no attribute 'ping'
# end::UNRELATED_DEMO_1[]
But if ``U`` is part of a cooperative arrangement of base classes,
its ``ping`` method works::
# tag::UNRELATED_DEMO_2[]
>>> leaf2 = LeafUA()
>>> leaf2.ping()
<instance of LeafUA>.ping() in LeafUA
<instance of LeafUA>.ping() in U
<instance of LeafUA>.ping() in A
<instance of LeafUA>.ping() in Root
>>> LeafUA.__mro__ # doctest:+NORMALIZE_WHITESPACE
(<class 'diamond2.LeafUA'>, <class 'diamond2.U'>,
<class 'diamond.A'>, <class 'diamond.Root'>, <class 'object'>)
# end::UNRELATED_DEMO_2[]
Here ``U.ping`` is never called because ``Root.ping`` does not call ``super``.
>>> o6 = LeafAU()
>>> o6.ping()
<instance of LeafAU>.ping() in LeafAU
<instance of LeafAU>.ping() in A
<instance of LeafAU>.ping() in Root
>>> LeafAU.__mro__ # doctest:+NORMALIZE_WHITESPACE
(<class 'diamond2.LeafAU'>, <class 'diamond.A'>, <class 'diamond.Root'>,
<class 'diamond2.U'>, <class 'object'>)
"""
# tag::DIAMOND_CLASSES[]
from diamond import A # <1>
class U(): # <2>
def ping(self):
print(f'{self}.ping() in U')
super().ping() # <3>
class LeafUA(U, A): # <4>
def ping(self):
print(f'{self}.ping() in LeafUA')
super().ping()
# end::DIAMOND_CLASSES[]
class LeafAU(A, U):
def ping(self):
print(f'{self}.ping() in LeafAU')
super().ping()