@property decorator define property accessor methods getter in Section 7.26 Python, setter, deleter Detailed

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 )

@property decorator define property accessor methods getter in Section 7.26 Python, setter, deleter Detailed

I. Introduction
Python decorators in front of the contact, the old ape has not expanded in-depth introduction decorator relevant content, in separate chapters later to introduce. Comprises a total of three built Python decorators (abstractmethod note is imported from the decorative abc module, not built-in), in addition to the class methods previously described classmethod decorators, decorators staticmethod static method, there is a property decorator, the purpose of the decorator and the property function is the same, just on a different use.

Second, the grammatical interpretation decorator property of
Python decorators in property and property functions, is accessible property will be converted to the corresponding method, and therefore property function, decorators also provides a get, set, del method attribute. Are defined before the relevant statements in the method using the class definition, the specific syntax is as follows:
. 1, GET decorator:
@Property
DEF attribute name (self):
    Method Code
return a return value attribute

        Syntax Definition:
1) the relevant code execution when decorators get used to access the property name, this must be built @property decorator to define, get method all the decorations properties must be built using the decorator. That is @property decorative way is to get the property value of the method is the name of the decorating method will be used as the property name;
2) the property name and property returned here instance property is a concept used elsewhere in the property were accessed, the property name must be behind the definition of the name of the method and set methods del;
3) returns the attribute value through a method code may be treated, an instance may be the instance variables or variable calculation of value, if the direct return an instance variable, then the method code no.

2, set decorator:
@ attribute name .setter
DEF attribute name (self, value):
     Method Code


     Setting an instance variable to hold the value or values through the calculation of the value
Syntax Interpretation:
1) of this section is decorated @ attribute name .setter, attribute name is specified for the previous defined @property name attribute get method, you must be the same. "Setter" is used for specifying the decoration set decorator, fixing "setter". Garnished @ attribute name .setter method is to set the property value method;
2) def note the name of the function name must match the name attribute, in which case, get defined attributes, set, function names are del property name, introduced earlier this Python does not support overloaded function appears to be contradictory, but in fact the decoration itself is also a function behind the decorator decorator nested function is the function itself, and therefore its scope confined to the decorator function, thus there is no case where the same name covered;
. 3) value is the value of the property to be set, if the attribute is obtained through the operation, the assignment need to push down the reverse, if the value saved to function directly instance variables, the method code assignment can be only one.

3, del decorator:
@ attribute name .deleter
DEF attribute name (self):
     Method Code

1) @ attribute method name is .deleter decoration is to remove the property value method, property name must also be in front of the get method defined attribute name, "deleter" is fixed, its role is in external call "del attribute name "when the method of execution as a delete operation.
2) def Note the name of the function name must match the name attribute.
  
Note:
1. Adoption of the @property definition of a property, get a decorator is necessary to define, set and del to see if decorators need to confirm whether the definition, if not defined setter, the property can not be assigned, if not defined deleter, then the property can not be deleted.
2. decorator defined property name and not the name of the same class instance variables, the reason described in the previous section reason property function is the same as defined attributes. For details, please refer to this blog's "Comments Section 7.26 Python Case: Use attribute property function definitions and instance variables of the same name will happen? "

This section describes in detail the decorator property syntax, attention must define at least a getter decorative value attribute for reading through the decorative property definition property. Old ape that, in fact on property and property decorator function is essentially the same, just different syntax definitions. The following section will combine a case of the above knowledge point for further introduction.
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/93385341