I have the following class:
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))
Setting kind causes the maximum recursion depth exceeded aka stack overflow.
Q: How to re-write the setter to make it work with fixed list only?
__slots__instead... Either that or__setattr__... perhaps if you explained in a paragraph what you want to achieve... - Jon Clements♦self.kind = xin the setter calls the setter again? Use a different name for the underlying attribute and the property. - millimoose@propertycorrectly in general. - millimooseself.kind = xcalls the setter again, etc). Use a "private" attribute instead, e.g.self._kind. - Henry Keiterkinds_listinstead of just definingTank,Car, [etc] classes that inherit fromvehicle? - Henry Keiter