-
Notifications
You must be signed in to change notification settings - Fork 0
/
inheritance.py
72 lines (60 loc) · 1.81 KB
/
inheritance.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
67
68
69
70
71
72
class User:
# this class doesn't have an __init__
def sign_in(self):
return 'logged in'
# this is the way to achieve inheritance. Wizard is subclass of USer
class Wizard(User):
def __init__(self, name, power):
self.name = name
self.power = power
def attack(self):
return f'attacking with power of {self.power}'
# Archer is also a derived/sub class of User
class Archer(User):
def __init__(self, name, num_arrows):
self.name = name
self.num_arrows = num_arrows
def attack(self):
return f'attacking with arrows: arrows-left are {self.num_arrows}'
w1 = Wizard('Merlin', 50)
a1 = Archer('Robinhood', 100)
print(w1.sign_in()) # logged in
print(w1.attack()) # attacking with power of 50
print(a1.sign_in()) # logged in
print(a1.attack()) # attacking with arrows: arrows-left are 100
# in python, we also have a useful function called isinstance()
# which can be used to check whether the object is the instance
# of the respective class, or not.
print(isinstance(w1, Wizard)) # True
print(isinstance(a1, Archer)) # True
print(isinstance(a1, Wizard)) # False
print(isinstance(w1, Archer)) # False
# Everything in python is an object, it literally means that
# every python data type like list, tuple, dict, etc and user-
# defined objects are an instance of the top-most super class
# called object.
# Therefore, all of the follwing will result True
print(isinstance(w1, object)) # True
print(isinstance(a1, object)) # True
print(isinstance([], object)) # True
print(isinstance(tuple, object)) # True
print(isinstance(dict, object)) # True
print(isinstance(set, object)) # True
'''
Output:
------
logged in
attacking with power of 50
logged in
attacking with arrows: arrows-left are 100
True
True
False
False
True
True
True
True
True
True
'''