Use of property in python

property attribute

definition

A special keyword that can make an instance method be used like an instance attribute can correspond to a method. By using the property attribute, it can simplify the caller's process of obtaining data (make the code more concise).

The definition and call of property attributes should pay attention to the following points:

When calling, there is no need for parentheses, adding it is wrong; and there is only one self parameter

Two ways to implement property attributes

Decorator

The attributes in the new-style class have three access methods, and they correspond to three

  • @property corresponding read
  • @Method name.setter modification
  • @Method name.deleter delete attribute

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

class Goods:

 def __init__(self):

 self.age = 18

  

  @property

  def price(self): # 读取

    return self.age

     

  # 方法名.setter

  @price.setter # 设置,仅可接收除self外的一个参数

  def price(self, value):

    self.age = value

     

  # 方法名.deleter

  @price.deleter # 删除

  def price(self):

    del self.age

 

# ############### 调用 ###############

obj = Goods()  # 实例化对象

obj.age  # 直接获取 age属性值

obj.age= 123   #  修改age的值

del obj.age  #  删除age属性的值

Class attribute

When using class attributes to create property attributes, the property() method has four parameters

  • The first parameter is the method name, which automatically triggers the execution of the method when calling the object. property
  • The second parameter is the name of the method, calling the object. The method is automatically triggered when the attribute = XXX
  • The third parameter is the method name, which automatically triggers the execution of the method when calling the del object.
  • The fourth parameter is a string, calling object.attribute.doc, this parameter is the description of the attribute

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

class Goods(object):

  def __init__(self): 

    self.price = 100  # 原价

    self.discount = 0.8 # 折扣

 

  def get_price(self):

    # 实际价格 = 原价 * 折扣

    new_price = self.price * self.discount

    return new_price

 

  def set_price(self, value):

    self.price = value

 

  def del_price(self):

    del self.price

   # 获取    设置     删除    描述文档

  PRICE = property(get_price, set_price, del_price, '价格属性描述...')

 # 使用此方式设置

  

obj = Goods()

obj.PRICE     # 获取商品价格

obj.PRICE = 200  # 修改商品原价

del obj.PRICE   # 删除商品原价

Use property instead of getter and setter methods

Use @property decorator to improve the get and set methods of private properties

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

class Money(object):

  def __init__(self):

    self.__money = 0

 

  # 使用装饰器对money进行装饰,那么会自动添加一个叫money的属性,当调用获取money的值时,调用装饰的方法

  @property

  def money(self):

    return self.__money

 

  # 使用装饰器对money进行装饰,当对money设置值时,调用装饰的方法

  @money.setter

  def money(self, value):

    if isinstance(value, int):

      self.__money = value

    else:

      print("error:不是整型数字")

 

a = Money()

a.money = 100

print(a.money)

Guess you like

Origin blog.csdn.net/grl18840839630/article/details/110135154