fromtypesimportMethodType
classStudent(object):
def__init__(self, *args, **kwargs):
self.name= kwargs.pop('name')
self.age= kwargs.pop('age')
self.score= kwargs.pop('score')
defset_score(self, value):
self.score = value
defget_score(self):
returnself.score
Student.set_score =MethodType(set_score, Student)
Student.get_score =MethodType(get_score, Student)
t=Student(name="Bryan", age=24, score=80)
# 输出80
print(t.score)
t.set_score(100)
# 输出100
print(t.get_score())
# 为什么这里还是输出80,而不是100?
Sign in to make a reply
你曾温润如玉
fromtypesimportMethodType
classStudent(object):
def__init__(self, *args, **kwargs):
self.name= kwargs.pop('name')
self.age= kwargs.pop('age')
self.score= kwargs.pop('score')
defset_score(self, value):
self.score = value
defget_score(self):
returnself.score
Student.set_score =MethodType(set_score, Student)
Student.get_score =MethodType(get_score, Student)
t=Student(name="Bryan", age=24, score=80)
# 输出80
print(t.score)
t.set_score(100)
# 输出100
print(t.get_score())
# 为什么这里还是输出80,而不是100?
print(t.score)