当前位置: 首页 > news >正文

python描述符装饰器 property的联合套用

python描述符装饰器 property的联合套用

def get_discount_price(f):def _f2(self):return f(self)*self.discountreturn _f2
class Product:def __init__(self, price, discount):self.price = priceself.discount = discount@property@get_discount_price   def discount_price(self):return self.pricep = Product(100, 0.9)
# print(p.get_price())
print(p.discount_price)


运行结果:

image