-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultiple-inheritance.py
More file actions
57 lines (35 loc) · 1.36 KB
/
Copy pathmultiple-inheritance.py
File metadata and controls
57 lines (35 loc) · 1.36 KB
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
# Multiple inheritance means a class can inherit from more than one parent class,
# gaining attributes and methods from all of them.
#Basic example:
class A:
def method_a(self):
print("Method from A")
class B:
def method_b(self):
print("Method from B")
class C(A, B): # C inherits from both A and B
pass
obj = C()
obj.method_a() # Method from A
obj.method_b() # Method from B
-----------------------------------
-----------------------------------
class Employee:
def __init__(self, name):
self.name = name
def show(self):
print(f"The name of the Employee is {self.name}")
class Dancer():
def __init__(self, dance_type):
self.dance_type = dance_type
def show(self):
print(f"The dance type of the Dancer is {self.dance_type}")
class DancerEmployee(Employee, Dancer):
def __init__(self, name, dance_type):
Employee.__init__(self, name)
Dancer.__init__(self, dance_type)
o = DancerEmployee("Shamser", "Hip Hop")
o.show() # This will call the show method from Employee class due to method resolution order
Dancer.show(o) # This will call the show method from Dancer class
Employee.show(o) # This will call the show method from Employee class
print(DancerEmployee.__mro__) # This will print the method resolution order for DancerEmployee class