Section 7.27 Python Case Comments: @property decorator define property access method getter, setter, deleter

Creative Commons License Copyright: Attribution, allow others to create paper-based, and must distribute paper (based on the original license agreement with the same license Creative Commons )

Details of the previous section using the syntax definition @property decorative properties, this section is further illustrated by the specific case.
First, the case illustrates
the case in this section is to define Rectangle (rectangular) type, to illustrate the problem, in addition to the constructor, another method only defines the operative length attribute. Including three full decorator, if you want to try a few decorator method of operation, may correspond directly to the decorator can be removed.

Second, the code Case
1. Define class Rectangle

class Rectangle():
    def __init__(self,length,width): self.width,self.length = width,length
   
    @property  #定义getter装饰器
    def len(self):
        print("execute getLen")
        return self.length
       
    @len.setter   #定义setter装饰器
    def len(self,length):
        print("execute setLen")
        self.length=length
       
    @len.deleter  #定义deleter 装饰器
    def len(self):self.length=0

2. The definition of the instance object and perform related operations (performed interactively)

rect = Rectangle(5,3)
rect.len  #获取长方形的长
rect.len=10 #设置长方形的长
rect.len  #获取长方形的长
del rect.len #删除长方形的长

As can be seen from the execution screenshot below, the relevant operations are performed corresponding to the method call, only the name of the method must be fixed property name.
Third, the case shot
 
four anomalies code and screenshots
with the "Section 7.25 Python Case Comments: Use the attribute property function definitions and instance variables of the same name will happen? "Like the old ape also tested property names anomalies at the same time confirmed the unusual circumstances and unusual circumstances described in section 7.25 exactly the same. Screenshot relevant code portion and executed as follows:

#property装饰器设置与类变量的同名属性 
class Rectangle():
    def __init__(self,length,width): self.width,self.length = width,length
   
    @property
    def length(self):
        print("execute getLen")
        return self.length
       
    @length.setter    
    def length(self,length):
        print("execute setLen")
        self.length=length
    @length.deleter
    def length(self):self.length=0
       
rect = Rectangle(5,3)

Screenshot execution:

By performing the above code in this verification can not be the same name as the attribute name and instance variables, except for using a variable does not have any instance data operation.
This section old ape with case details the use of property decorator, decorator and illustrate the definition of property can not be the same name as the class and instance variables, as this may cause an exception. As of the basics of this section, classes and types of speaking majority, as well as some behind to open a separate section to reports, the chapter ends.
Old ape Python (https://blog.csdn.net/LaoYuanPython) series of articles for the gradual introduction summary of the old ape learning Python learning experience, which helps no contact with Python programmers can easily enter Python world.
Welcome criticism, thank you attention!

Guess you like

Origin blog.csdn.net/LaoYuanPython/article/details/93385612