-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOOP.py
More file actions
184 lines (133 loc) · 4.98 KB
/
Copy pathOOP.py
File metadata and controls
184 lines (133 loc) · 4.98 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
"""
--------------------------------------OBJECT ORIENTED PROGRAMMING-----------------------------------------------------
TOPICS COVERED
1. INTRO TO CLASS
2. NOT EVERYTHING TO BE CLASS
3. DATA AND BEHAVIOUR
4. CREATE YOUR FIRST CLASS
5. CLASS INSTANCES
6. ADDING METHODS OR BEHAVIOUR TO YOUR PYTHON CLASS
7. MAGIC METHODS
8. INHERITANCE
9. POLYMORPHISM
10. CLASS METHODS
11. STATIC METHODS
12. PROPERTY
13. GETTERS
14. SETTERS
------------------------------------------------------------------------------------------------------------------
"""
"""
--------------------------------------INTRO TO CLASS-----------------------------------------------------
IT IS USED WHEN NEED TO USE BOTH STRUCTURE AND BEHAVIOUR
Inheritance:
------------
All species inherit a lot from their parents, may be they have same eyes as
their mother but different voice all together.
Python classes are no different, we can inherit from classes and make them share
common functionality. At the same time we can dynamically add other features and
functionality as well.
Polymorphism:
-------------
Suppose there are two children, one want's to speak in Marathi, other want's to
speak in Sanskrit. This is possible using polymorphism! It's just creating the same
methods but with different behavior.
------------------------------------------------------------------------------------------------------------------
"""
class ParentClass:
def __init__(self, name, age) -> None:
self.name = name
self.age = age
def __str__(self) -> str:
return f"Name: {self.name}"
def __repr__(self) -> str:
return f"Class - Parent"
def isAuthenticated(self) -> bool:
return self.age >= 18
"""
print(ParentClass.isAuthenticated) # <function ParentClass.isAuthenticated at 0x10e5a31f0>
print(ParentClass.__str__) # <function ParentClass.__str__ at 0x10fd15940>
print(ParentClass.__repr__) # <function ParentClass.__repr__ at 0x10fd158b0>
instance = ParentClass("Myilvaganan", 29)
print(instance.isAuthenticated())
instance2 = ParentClass("Ram", 17)
print(instance2.isAuthenticated())
"""
class Dob(ParentClass):
def __init__(self, name: str, age: int, height: int, weight: int) -> None:
super().__init__(name, age)
self.height = height
self.weight = weight
def __str__(self) -> str:
return f"Name: {self.name}, age: {self.weight}"
def getBMI(self) -> str:
bmi: float = self.weight / self.height * self.height
return f"{self.name} BMI is: {bmi}"
a1 = ParentClass("Myilvaganan", 12)
a = a1.isAuthenticated()
print(a)
c1 = Dob("Myilvaganan", 17, 171, 89)
print(c1.getBMI())
class Animal:
def __init__(self, name: str, age: int, num_legs: int) -> None:
# Create & initialize instance variables
self.name = name
self.age = age
self.num_legs = num_legs
def __str__(self) -> str:
return f"Name: {self.name}"
def talk(self) -> None:
"""Makes the animal talk"""
print(f"{self.name} can't talk yet!")
a1 = Animal("Robbin", 10, 4)
print(a1)
class Dog(Animal):
def __init__(self, name: str, age: int, num_legs: int, breed: str) -> None:
# Take the common features and pass to the parent(super) class
super().__init__(name, age, num_legs)
# Define custom instance variables
self.breed = breed
self.type = "Dog"
def __str__(self) -> str:
return f"{self.type}: {self.name}, Breed: {self.breed}"
# We alter the talk method and make it say woff adding polymorphic behavior
def talk(self) -> None:
print(f"{self.name} says wofff...")
def sniff(self, item: str) -> None:
print(f"{self.name} is sniffing out {item}")
d1 = Dog("Whisky", age=5, num_legs=4, breed="Doberman")
print(d1)
d1.talk()
d1.sniff("ball")
class Cat(Animal):
def __init__(self, name: str, age: int, num_legs: int, breed: str) -> None:
super().__init__(name, age, num_legs)
self.breed = breed
self.type = "Cat"
def __str__(self) -> str:
return f"{self.type}: {self.name}, Breed: {self.breed}"
def talk(self) -> None:
print(f"{self.name} says meow...")
c1 = Cat("Jess", age=2, num_legs=4, breed="Persian Cat")
print(c1)
c1.talk()
class Dino(Animal):
def __init__(self, name: str, age: int, num_legs: int, breed: str) -> None:
super().__init__(name, age, num_legs)
self.breed = breed
self.type = "Dino"
def __str__(self) -> str:
return f"{self.type}: {self.name}, Breed: {self.breed}"
def talk(self) -> None:
print(f"{self.name} says urrghhhh...")
def hunt(self) -> None:
print(f"{self.name} is out for hunting...")
dino1 = Dino("Adam", age=8, num_legs=2, breed="T-Rex")
print(dino1)
dino1.talk()
dino1.hunt()
# -------------------------------------------------------------------------
print(isinstance(d1, Animal))
print(isinstance(d1, Dog))
print(isinstance(d1, Cat))
# -------------------------------------------------------------------------