我正在学习python食谱。 我试图执行下面的代码,这是从蟒食谱第8章。 此代码是有关类在Python中制作类的属性
class Person:
def __init__(self, first_name):
self.first_name=first_name
#getter function
#property
def first_name(self):
return self._first_name
#settier function
def first_name(self, value):
print(value, isinstance (value,str))
if not isinstance(value, str):
raise TypeError("expected a string")
self._first_name=value
#deleter function
def first_name(self):
raise AttributeError("can not delete attribute")
c=Person('PETTER')
c.first_name(42)
的特性使得下课后,我做了实例,我进入上目的错误值。
我希望我得到了TypeError(预期是一个字符串)。 但我没有。 我的代码的哪些部分应该更改?
我投票细节关闭这个问题作为题外话b因为SO不是SolveMyExerciseForMe.com –