-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrade.py
44 lines (33 loc) · 1.05 KB
/
grade.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
from weakref import WeakKeyDictionary
# class Grade:
# def __init__(self) -> None:
# self.grade = 0
# @property
# def grade(self):
# return self.grade
# @grade.setter
# def grade(self, value:str):
# if not (0<= value <= 100):
# raise ValueError("must be between 0 and 100")
# self.grade = value
class Grade:
def __init__(self) -> None:
self.values = WeakKeyDictionary()
def __get__(self, instance, instance_type):
if instance is None:
return self
return self.values.get(instance, 0)
def __set__(self, instance, value):
if not (0<= value <= 100):
raise ValueError("must be between 0 and 100")
self.values[instance] = value
class Exam:
math = Grade()
history = Grade()
computer = Grade()
first_exam = Exam()
first_exam.math = 50
second_exam = Exam()
second_exam.computer = 90
print(f'first exam is {first_exam.math}')
print(f'second exam is {second_exam.computer}')