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

def get_discount_price(f):
    def _f2(self):
        return f(self)*self.discount
    return _f2
class Product:
    def __init__(self, price, discount):
        self.price = price
        self.discount = discount

    @property
    @get_discount_price   
    def discount_price(self):
        return self.price


p = Product(100, 0.9)
# print(p.get_price())
print(p.discount_price)


运行结果:

image

posted on 2025-12-10 16:42  Angry_Panda  阅读(1)  评论(0)    收藏  举报

导航