-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathClasses.py
212 lines (145 loc) · 4 KB
/
Classes.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
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
print(type(5))
my_dict = {}
print(type(my_dict))
my_list = [num for num in range(100)]
print(type(my_list))
print(my_list)
class Circle():
# constant
pi = 3.14
# method
def area(self, radius):
return self.pi * radius ** 2
# Python Class
class CoolClass:
pass
class Facade:
pass
# Class instatiation
class Facade:
pass
# a Facade instance
facade_1 = Facade()
# Object-oriented programming
class Facade:
pass
facade_1 = Facade()
facade_1_type = type(facade_1)
print(facade_1_type)
# Class variables
class Grade:
minimum_passing = 65
# Methods
class Rules():
# method
def washing_brushes(self):
return "Point bristles towards the basin while washing your brushes."
# Methods with Arguments
class Circle():
# constant
pi = 3.14
# method
def area(self, radius):
return self.pi * radius ** 2
circle = Circle()
pizza_area = circle.area(12 * 0.5)
teaching_table_area = circle.area(36 * 0.5)
round_room_area = circle.area(11460 * 0.5)
big_circle = Circle()
print(big_circle.area(50))
pizza_area = big_circle.area(12 * 0.5)
teaching_table_area = big_circle.area(36 * 0.5)
round_room_area = big_circle.area(11460 * 0.5)
print(pizza_area, teaching_table_area, round_room_area)
# Constructors
class Circle:
pi = 3.14
# Add constructor here:
def __init__(self, diameter):
print("New circle with diameter: " + str(diameter))
teaching_table = Circle(36)
# Instant Variables
class Store:
pass
alternative_rocks = Store()
isabelles_ices = Store()
alternative_rocks.store_name = "Alternative Rocks"
isabelles_ices.store_name = "Isabelle's Ices"
# Attribute Functions
how_many_s = [{'s': False}, "sassafrass", 18, ["a", "c", "s", "d", "s"]]
"""
Python functions that checks for attibutes within an object
hasattr(attributeless, "fake_attribute")
# returns False
getattr(attributeless, "other_fake_attribute", 800)
# returns 800, the default value
"""
s_count = 0
for item in how_many_s:
if hasattr(item, "count") == True:
for x in item:
if x == "s":
s_count += 1
print(s_count)
# self
class Circle:
pi = 3.14
def __init__(self, diameter):
print("Creating circle with diameter {d}".format(d=diameter))
# Add assignment for self.radius here:
self.radius = 0.5 * diameter
def circumference(self):
return 2 * self.pi * self.radius
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
print(medium_pizza.circumference())
print(teaching_table.circumference())
print(round_room.circumference())
# Everything is an object
print(dir(5))
def this_function_is_an_object(num):
return "Cheese is {} times better than everything else".format(num)
print(dir(this_function_is_an_object))
# String representation
class Circle:
pi = 3.14
def __init__(self, diameter):
self.radius = diameter / 2
def __repr__(self):
return "Circle with radius {radius}".format(radius=self.radius)
def area(self):
return self.pi * self.radius ** 2
def circumference(self):
return self.pi * 2 * self.radius
medium_pizza = Circle(12)
teaching_table = Circle(36)
round_room = Circle(11460)
print(medium_pizza)
print(teaching_table)
print(round_room)
# Review
# 1: Define a class named Student
class Student():
# 2: Add a constructor for Student, e.g. name and year
def __init__(self, name, year):
self.name = name
self.year = year
# 6 ...
self.grades = []
def add_grade(self, grade):
if type(grade) is Grade:
self.grades.append(grade)
else:
pass
# 4: Create a Grade class with minimum passing = 65
class Grade():
minimum_passing = 65
# 5: Give grade a constructor, param is score
def __init__(self, score):
self.score = score
# 3: Create three instances of the Student class:
roger = Student("Roger van der Weyden", 10)
sandro = Student("Sandro Botticelli", 12)
pieter = Student("Pieter Bruegel the Elder", 8)
pieter.add_grade(Grade(100))