-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass_property.py
42 lines (31 loc) · 1.02 KB
/
class_property.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
# class Student:
# def __init__(self,phy,chem,math):
# self.phy = phy
# self.chem = chem
# self.math = math
# self.percentage = str((self.phy +self.chem + self.math)/3)+"%"
# def calcPercen tage(self):
# self.percentage = str((self.phy +self.chem + self.math)/3)+"%"
# stu1 = Student(98,97,99)
# print(stu1.percentage)
# stu1.phy = 86
# print(stu1.phy)
# print(stu1.percentage)
# stu1.calcPercentage()
# print(stu1.percentage)
# better way to do the above is
class Student:
def __init__(self,phy,chem,math):
self.phy = phy
self.chem = chem
self.math = math
@property
def percentage(self):
return str((self.phy +self.chem + self.math)/3)+"%"
stu1 = Student(98,97,99)
print(stu1.percentage)
stu1.phy = 86
print(stu1.phy)
print(stu1.percentage)
# now when we change the marks of physics the percentage will also automatically change. we dfont have to amke anither method to do this
# this is because we made it into a property