Section 7.25 Python Case Comments: Use the attribute property function definitions and instance variables of the same name will happen?

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.25 Python Case Comments: Use the attribute property function definitions and instance variables of the same name will happen?

 

First, the case illustrates
that we mentioned in the previous section, using the property function defined attributes not common within the class instance variables with the same name already defined, if the same name is what will happen then? One example of this case by the same name illustrate the consequences that may arise, but also want to show custom properties access property What will happen in the class.
To illustrate the problem directly, or in this case more than Rectangle festival as an example, but most of the properties and methods removed, leaving only the necessary code can be telling.
Second, the code Case
1. Define class, the class definition of the instance variables in vivo self.length, and defines the class constructor, setLen, getLen method;

class Rectangle():
   def __init__(self,length,width): self.width,self.length = width,length
   def setLen(self,length):
       print("execute setLen")
       self.length=length
   def getLen(self):
       print("execute getLen")
       return self.length


2. property attributes defined length, this property is set to be read, the instance variables with the same variable name self.length;   

length = property(getLen, setLen,None,'长方形的长')

3. Examples of objects defined

rect = Rectangle(5,3)

After the execution, we see that the system kept output "execute setLen", repeated many times after the newspaper "RecursionError: maximum recursion depth exceeded while pickling an object". Screenshot partially implemented:
 


 
Third, the Case Analysis
1. Analysis
of the problem the old ape little early analysis for the following reasons:
1) It can be seen from the above error message, the error is triggered when setLen define instance variables because the constructor is executed on the instance variable assignment self.length method, and setLen will lead to the implementation of self.length assignment setLen kept recursive call, eventually leading to resource depletion was terminated;
2) Why self.length assignment will trigger setLen call it, certainly because custom attributes and instance variables of the same name caused. But why is it so old ape did not understand, as detailed in doubt following the old ape.
2. The old ape wondering
why the definition of the properties and instance variables of the same name will trigger recursive calls? Old ape temporarily unable to in-depth analysis, there are two problems in this old ape did not understand:
1) the definition of instance attributes property, which is defined statement is executed in the class body, intuitively, this property is defined in the instance of a class variable, special type which is the type of property, but according to this example of the access attribute situation is accessible by way of example, and the interference between different instances of objects, so this example should not be property class variables, this property is not know how to achieve?
2) The second question is what is the main type of property attributes, and instance variables associated with it, we know it has four parameters, three methods are property of getter (), setter () and delete () as use, but which property records examples of these methods accessible?
This section describes the use of property definitions and instance variables of the same properties, find this definition would cause the trigger recursive call and quit unexpectedly, indicating:
1, can not use an instance property is defined attributes and instance variables of the same name;
2, the instance attributes defined in any class and classes outside triggers associated access method is performed, so we must pay attention when accessing the property, preventing occurrence of this embodiment similar to the recursive call.
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/93351066