Error message
• When we use the property decorator, the following errors may be caused by the wrong writing of the decorator Name:
TypeError: descriptor 'setter' requires a 'property' object but received a 'function'
Problem analysis
• The reason for this error is that we write all decorator names as property instead of the same method name in our class:
Problem code
class AgeDemo(object):
def __init__(self, age):
self.age = age
@property
def age_test(self):
return self.age
@property.setter # error
def age_test(self, age):
if not isinstance(age, int):
raise TypeError('TypeError')
self.age = age
Resolve error reporting
• Change the name of the decorator with an error in the figure to the same name
set in our class to solve this error
the code is as follows:
class AgeDemo(object):
def __init__(self, age):
self.age = age
@property
def age_test(self):
return self.age
@age_test.setter # 修改的代码行
def age_test(self, age):
if not isinstance(age, int):
raise TypeError('TypeError')
self.age = age