Python using the property function defined property name with the same name as another instance variables What happens?

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 )

If the first instance of an object of the same name attribute attribute name defined in the corresponding operating method of operation will trigger an infinite recursive calls, please refer to the relevant part of the "Python Case Comments: Use the attribute property function definitions and instance variables of the same name will happen? "
But if the same name is defined as a different instance variable name it? We look at the case:

   
>>> 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
   width = property(None,setLen,0,'长方形的长')#属性名与实例变量width同名并且未设置get方法

   
>>> rect=Rectangle(10,5)
execute setLen
>>> rect.width
Traceback (most recent call last):
  File "<pyshell#35>", line 1, in <module>
    rect.width
AttributeError: unreadable attribute
>>> rect.width=15
execute setLen
>>> 
>>> 

As can be seen from the above cases, the properties defined width and width instance variables of the same name, the real instance variable is mapped to len, when executed property width of view, the properties can not read packets, and performing an assignment property, is performed setLen method, equivalent to the original width instance variables completely covered.

Related content may refer to:
. 1, "the Python using code property function defined attributes for attribute an accessible"
2, "the Python case Explanation: using the property function defined attributes for attribute access code implementation"
. 3, "the Python case Explanation: using the property function is defined and instance variables of the same name attribute what will happen? "
4, " Python Case Detailed: @property decorator define property access method getter, setter, deleter "
5, " the @Property decorator define property accessor methods getter Python in, setter, deleter Detailed "
6, " the use of property Python function and similarities and differences using @property decorator define property accessor methods of analysis. "

Old ape Python, with the old ape learn Python!
Blog address: https: //blog.csdn.net/LaoYuanPython

please support, thumbs up, comment and processing concern! Thank you!

Guess you like

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