-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paths8_python_oop.py
More file actions
216 lines (140 loc) · 4.18 KB
/
s8_python_oop.py
File metadata and controls
216 lines (140 loc) · 4.18 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# For class we follow CamelCasing
# For variable names and function names we follow snake_casing
# define class
class Sample():
pass
# create an instance of class
my_sample = Sample()
# check type
print(type(my_sample))
'''
class Dog():
# self connects this method to instance of class
def __init__(self, mybreed):
# We take in the argument
# Assign it to the instance using self.attribute_name
self.my_attribute = mybreed
my_dog = Dog(mybreed='Lab')
my_dog.my_attribute
'''
# define dog class
class Dog():
# class object atributes
# same for any instance of a class
species = 'Mammal'
# self connects this method to instance of class
def __init__(self, breed, name, spots):
self.breed = breed
self.name = name
# Expect boolean True/False
self.spots = spots
# operations / Actions --> Methods (basically loook lika functions inside class)
# Method is a function that is inside class
# Function is outside a class
def bark(self, number):
print("WOOF! My name is {} and the number is {}".format(self.name, number))
print(f"WOOF! My name is {self.name} and the number is {number}")
# my_dog = Dog(breed='Lab',name='Sammy',spots=False)
my_dog = Dog('Lab', 'Sammy', False)
print(type(my_dog))
print(my_dog.breed, my_dog.name, my_dog.spots, my_dog.species)
my_dog.bark('22')
class Circle():
# Class object attribute
pi = 3.14
def __init__(self, radius=1):
self.radius = radius
# we can caculate an area
# instead of self.pi we can write Circle.pi
self.area = radius * radius * self.pi
# Method
def get_circumference(self):
return self.radius * self.pi * 2
# example with the use of default value
my_circle = Circle()
print('Circle 1 area: ', my_circle.area)
print('Circle 1 circumference: ', my_circle.get_circumference())
my_circle = Circle(22)
print('Circle 2 area: ', my_circle.area)
print('Circle 2 circumference: ', my_circle.get_circumference())
# Inheritance and Polymorphism
# Create base class
class Animal():
def __init__(self):
print('Animal created')
def who_am_i(self):
print('I am animal')
def eat(self):
print('I am eating')
# derived class - Cat()
class Cat(Animal):
def __init__(self):
Animal.__init__(self)
print('Cat Created')
# we can overwrite method
def who_am_i(self):
print('I am a cat')
my_animal = Animal()
my_animal.who_am_i()
my_animal.eat()
my_cat = Cat()
my_cat.who_am_i()
my_cat.eat()
# Polymorphism
class Mouse():
def __init__(self, name):
self.name = name
def speak(self):
return self.name + " says pii!"
class Hamster():
def __init__(self, name):
self.name = name
def speak(self):
return self.name + " says puff!"
niko = Mouse('Niko')
felix = Hamster('Felix')
print(niko.speak())
print(felix.speak())
for pet in [niko, felix]:
print(type(pet))
print(type(pet.speak()))
def pet_speak(pet):
print(pet.speak())
pet_speak(niko)
pet_speak(felix)
class AnimalBase():
def __init__(self, name):
self.name = name
def speak(self):
raise NotImplementedError('Subclass must implement this abstract method')
my_animal = AnimalBase('Ted')
# my_animal.speak()
class Rabbit(AnimalBase):
def speak(self):
return self.name + 'says woop!'
class Bunny(AnimalBase):
def speak(self):
return self.name + 'says woopwoop!'
robert = Rabbit('Robert')
rodger = Bunny('Rodger')
print(robert.speak())
print(rodger.speak())
# Real life examples with polymorphism - base class for opening failes (method - open) and subclasses for pdf open,
# xls open and other... share the same method name
# Special (Magic/Dunder) methods
class Book():
def __init__(self, title, author, pages):
self.title = title
self.author = author
self.pages = pages
def __str__(self):
return f'{self.title} by {self.author}'
def __len__(self):
return self.pages
def __del__(self):
print('A book object has been deleted')
b = Book('Python rocks', 'Jose', 200)
print(b)
print(str(b))
print(len(b))
del b