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

 

相关文章:

  • 2022-12-23
  • 2022-01-19
  • 2021-07-25
  • 2021-09-01
  • 2021-06-18
  • 2022-12-23
  • 2021-09-14
  • 2021-12-17
猜你喜欢
  • 2021-06-27
  • 2022-12-23
  • 2019-02-21
  • 2021-06-22
  • 2021-07-30
  • 2021-09-15
  • 2022-02-10
相关资源
相似解决方案