Section 7.23 Python using the property function defined attributes for attribute access code for

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 )

Section 7.23 Python using the property function defined attributes for attribute access code for

I. Background
      earlier in this chapter section, we introduce the related knowledge-based, and has been described for example, define a number of the form "get attributes", "SET Properties" in these examples is an instance attribute access method, when attribute instance data is private, access to that data can only be made by a method access method, but is not set to private instance data if you want to follow specific rules may only be accessed through the access method. However, if a program wants to access data more instances of large-scale use of this access method will make the program seem monotonous and tedious. To solve this problem, Python provides a mechanism to use as an ordinary class instance variables are accessed as access to these properties, and to ensure access methods using the class definition to access relevant, the equivalent of an assignment similar to an ordinary instance variables, query, delete access method, but the real implementation of the access method relies on the properties of the class definition. This function is to use the property to define an instance property access.

Second, interpretation of the syntax
1. syntax is defined for the class instance attributes defined body of code to add the following line statement:
Example Attribute = property (fget = None, fset = None, fdel = None, doc = None)


Defined, for example long rectangular access method getLen, setLen class Rectangle, and the length is stored in a rectangular instance variable is a private variable length even __length, we can use the following statement to define a new instance of the class attribute in accessing a rectangular length:
len = Property (getLen, setLen, None, 'long rectangular')

2. Syntax Interpretation
1) Examples of attributes: attribute name refers to later be accessed through this mode, the attribute name must use the instance properties of the new definition, you can not use properties that are already defined, after relying on the access related data attributes ; should be particularly noted that, in this example, a new attribute can be defined and associated with one or more instance variables, particularly by association methods to get and set setting, such as the example above, should have the following two methods in the class instances :
   

def setLen(self,length):self.__length=length
def getLen(self): return self.__length


2) The above method is defined attribute type instance attribute which is not an instance variable, but rather a type of "property" new object instance variables in real need access which will save a corresponding method of operation. property is not really a function, it is a class has many special methods, Property object has three methods, getter (), setter () and delete (), is used to set fget, fset and fdel method after the object is created. Its essence is to call property accessor methods define the properties of the new pair of camouflage access.
3) Parameters property four functions:
A) for the four parameters according to the order read attribute get method, set attribute setting method, del delete properties and methods doc, where the first three parameters respectively corresponding to the three examples of a method of certain properties or read, delete, and setting, doc string is a document, for explaining the newly defined attributes, the checks may be performed in the conversion and data processing methods in order to ensure the accuracy of data access;
b) in actual use, the incoming call may be property 0 (neither read nor write attribute), a (read-only attribute), 2 (read-write property), 3 (read-write property, It can also be deleted) and 4 (read-write property, can also be deleted, comprising documentation) parameter.
3. Define the get, set, del A method to operate one or some of instance variables and methods and operations of the binding properties with the property function, then the corresponding attribute is equivalent to the same instance variables can be accessed, access will automatically call related get, set, del method. Example:
We define a rectangular instance class: rect = Rectangle (5,4) after, by: print length information output (rect.len), by rect.len = 10 to adjust a length of 10. In fact, the above operation is equivalent to:
Print (rect.getLen ())
rect.setLen (10)
Examples of the above-described property is used to define access to a private property, one to one, actually define many attributes can be accessed and the original class instance variables defined (including private variables), can be subjected to the defined properties operation to change more than one instance variables, since they are all set to go through the assignment method, as assigned to whom, how the assignment method is set to decide.
4. If the attribute is defined corresponding to the internal property instance variables that can be accessed externally, access to the instance variable is not trigger these methods. If the above cases self .__ length (of course self .__ length can not be accessed directly, but if it is self.length) can be assigned from the outside, it is a direct instance variable assignment, the relevant set method does.

Three, function to define the property advantages of the instance attribute
1. property instance property is defined, the display manner, such as by calling the get, set class method access instance variables into use and assignment of instance attributes, which can effectively improve the code readability and simplicity, while ensuring proper access logic;
2. under certain scenarios, if the history of the code of instance variables using direct access method to use, but because of operational requirements, the need to increase the specific attribute access logic, in accordance with conventional implementations require access to all the variables into a get, set method, you need to modify a lot of code and a lot of testing, and the use of property can easily solve a problem, it can rename the original instance variables, instance variables with the corresponding original enhanced access method requires access logic control code, and then use the property name of the original is defined as the instance variables can be simply achieve these requirements.

This section uses the property function in a class definition for a convenient external access to the instance attribute grammar has been described and explained, it is noted that this newly defined attributes are read and write access by the internal method, which simplifies especially property access method assignments and deleted, while ensuring data access logic. The following section will combine detailed case 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/93350061