-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.py
More file actions
56 lines (36 loc) · 1006 Bytes
/
Copy pathexample.py
File metadata and controls
56 lines (36 loc) · 1006 Bytes
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
"""Inheritance and MRO — super(), multiple inheritance, method resolution order."""
class Animal:
def speak(self):
return "..."
class Flyer:
def move(self):
return "flying"
class Swimmer:
def move(self):
return "swimming"
class Duck(Animal, Flyer, Swimmer):
def speak(self):
return "quack"
def move(self):
return super().move()
class A:
def greet(self):
return "A"
class B(A):
def greet(self):
return f"B -> {super().greet()}"
class C(A):
def greet(self):
return f"C -> {super().greet()}"
class D(B, C):
def greet(self):
return f"D -> {super().greet()}"
if __name__ == "__main__":
print("=== MRO ===")
print(D.__mro__)
print(D().greet())
print("\n=== Duck — super picks Flyer.move (left-to-right MRO) ===")
d = Duck()
print(d.speak(), d.move())
print("\n=== isinstance / issubclass ===")
print(isinstance(d, Animal), issubclass(Duck, Flyer))