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

 

posted @ 2019-01-25 15:52  佛祖让我来巡山  阅读(117)  评论(0编辑  收藏  举报

佛祖让我来巡山博客站 - 创建于 2018-08-15

开发工程师个人站,内容主要是网站开发方面的技术文章,大部分来自学习或工作,部分来源于网络,希望对大家有所帮助。

Bootstrap中文网