/
class.py
84 lines (64 loc) · 2.15 KB
/
class.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
class LotteryPlayer(object):
def __init__(self, name, age):
self.name = age
self.age = age
self.number = (5, 9, 12, 3, 1, 21)
rob = LotteryPlayer("Rob", 23)
andy = LotteryPlayer("andy", 24)
class Student(object):
def __init__(self, name, school):
self.name = name
self.school = school
self.marks = []
def average(self):
return sum(self.marks) / len(self.marks)
def print_average_marks(self):
print(self.average())
@staticmethod
def static_to_school():
print("I'm going to school")
@classmethod
def student_to_school(cls):
student = cls("Unknown name", "Unknown school")
print("I'm going to school %s" % student.school)
def just_to_school(self):
print("I'm going to %s school" % self.school)
anna = Student("anna", "MIT")
anna.marks.append(56)
anna.marks.append(70)
anna.print_average_marks()
Student.student_to_school()
class Store(object):
def __init__(self, name):
self.name = name
self.items = []
def add_item(self, name, price):
self.items.append({
'name': name,
'price': price
})
def stock_price(self):
total = 0
for item in self.items:
total += item['price']
return total
@classmethod
def franchise(cls, store):
# Return another store, with the same name as the argument's name,
# plus " - franchise"
name = store.name + " - franchise"
new_store = cls(name)
return new_store
@staticmethod
def store_details(store):
# Return a string representing the argument
# It should be in the format 'NAME, total stock price: TOTAL'
return "{name}, total stock price: {total}".format(
name=store.name, total=store.stock_price())
store = Store("Test")
store2 = Store("Amazon")
store2.add_item("Keyboard", 160)
print(Store.franchise(store)) # -> a Store with name "Test - franchise"
print(Store.franchise(store2)) # -> a Store with name "Amazon - franchise"
print(Store.store_details(store)) # -> "Test, total stock price: 0"
print(Store.store_details(store2))