property相关补充

 1 # Author : Kelvin
 2 # Date : 2019/1/25 15:20
 3 class Foo:
 4     def __init__(self):
 5         self.original_price = 100
 6         self.discount = 0.8
 7 
 8     @property
 9     def price(self):
10         new_price = self.original_price * self.discount
11         return new_price
12 
13     @price.setter
14     def price(self, val):
15         self.original_price = val
16 
17     @price.deleter
18     def price(self):
19         del self.original_price
20 
21 
22 f = Foo()
23 print(f.price)
24 f.price = 200
25 print(f.price)
26 del f.price
27 print(f.__dict__)
28 
29 """输出结果:
30 80.0
31 160.0
32 {'discount': 0.8}
33 """

 

转载于:https://www.cnblogs.com/sun-10387834/p/10320059.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值