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 """

 

上一篇:python中深拷贝与浅拷贝


下一篇:AndroidStudio Gradle版本不匹配问题