我有以下课程:
class vehicle(object):
def __init__(self, name):
self.name = name
self.kinds_list = ["tank", "car", "motorbike", "bike", "quad" ]
@property
def kind(self):
return self.kind
@kind.setter
def kind(self, x):
if x in self.kinds_list:
self.kind = x
else:
raise AttributeError('No attribute {0} found !'.format(y))
设置 kind 会导致超出最大递归深度,也就是堆栈溢出。
问:如何重新编写 setter 使其仅适用于固定列表?