class Phone:
def __init__(self,brand,model_name,price):
self.brand = brand
self.model_name = model_name
self.price = price
@property
def price(self):
return self.price
@price.setter
def price(self,new_value):
self.price = max(new_value,0)
p1 = Phone('Asus','Asus10',102000)
print(p1.price)
错误:
RecursionError:超过最大递归深度
答案 0 :(得分:0)
属性名称和字段名称相同。在字段名称可以解决之前添加下划线。
class Phone:
def __init__(self,brand,model_name,price):
self.brand = brand
self.model_name = model_name
self._price = price
@property
def price(self):
return self._price
@price.setter
def price(self,new_value):
self._price = max(new_value,0)
p1 = Phone('Asus','Asus10',102000)
print(p1.price)