-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclasses_objects.py
More file actions
54 lines (42 loc) · 1.47 KB
/
classes_objects.py
File metadata and controls
54 lines (42 loc) · 1.47 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
lottery_player_dict = {
'name': 'Rolf',
'numbers': (5, 9, 12, 3, 1, 21)
}
class LotteryPlayer:
def __init__(self, name):
self.name = name
self.numbers = (5, 9, 12, 3, 1, 21)
def total(self):
return sum(self.numbers)
# Each new instance is distinct and not the same even if you compare using ==
player_one = LotteryPlayer("Rolf")
player_two = LotteryPlayer("Adam")
# print(player_one.name == player_two.name)
# print(player_one.numbers == player_two.numbers)
##
class Student:
def __init__(self, name, school):
self.name = name
self.school = school
self.marks = []
def average(self):
return (sum(self.marks)/len(self.marks))
# you can take self oyt by doing this adding cls classmethod
# @classmethod
# or you can pass in
@staticmethod # and you dont pass anything in as an arguemnt
def go_to_school():
# print("I'm going to {}".format(self.school))
print("I'm going to school")
# print("I'm a {}".format(cls))
anna = Student("Anna", "MIT")
anna.go_to_school()
# If we ran taking self out of the go to school method
# It will fail becuase you technically pass an argument
# in the name of the object itself, so you are passinbg in anna
Student.go_to_school() # can also call it by class itself
# anna.marks.append(56)
# anna.marks.append(71)
# anna.marks.append(63)
# print(anna.marks)
# print(anna.average())