A. Property property property using a static nature is to achieve a get, set, delete the three methods
class Foo: the @Property DEF AAA (Self): Print ( ' GET when I run ah ' ) @ AAA.setter DEF AAA (Self, value): Print (value, " *********** ******************************** " ) Print ( ' the SET when I run ah ' ) @ AAA.deleter DEF AAA (Self): Print ( ' the Delete when I run ah ' ) # can only be defined AAA.setter, AAA.deleter AAA attributes defined in the property f1 = Foo () f1.AAA f1.AAA =' aaa ' del f1.AAA # # GET when I run ah # run my ah set when # the Delete when I run ah
= Property AAA (get_AAA, set_AAA, delete_AAA) # built property three parameters with get, set, delete one to one class Foo: DEF get_AAA (Self): Print ( ' GET when I run ah ' ) DEF set_AAA (Self, value): Print ( ' the SET when I run ah ' ) DEF delete_AAA (Self): Print ( ' the Delete when I run ah ' )
AAA = property (get_AAA, set_AAA, delete_AAA) # built property three parameters and get, set, delete correspondence F1 = Foo () f1.AAA f1.AAA =' Aaa ' del f1.AAA # GET when I run ah # run my ah set when # the Delete when I run ah
class Goods: def __init__(self): # 原价 self.original_price = 100 # 折扣 self.discount = 0.8 @property def price(self): # 实际价格 = 原价 * 折扣 new_price = self.original_price * self.discount return new_price @price.setter def price(self, value): self.original_price = value @price.deleter def price(self): del self.original_price obj = Goods() obj.price # Gets commodity prices obj.price = 200 # modify original price of goods Print (obj.price) del obj.price # delete the original price of goods Case One
Reproduced in: https: //www.cnblogs.com/Sup-to/p/11090349.html