Explanation Section 7.24 Python Case: 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 )

Explanation Section 7.24 Python Case: using the property function defined attributes for attribute access code for

First, the case notes
This section describes how to use the shortcut through a case of property define property access. Using Rectangle case classes:
1, defined within the class of the length and width of two private attributes .__ length Self, Self .__ width;
2, defines a get method these two properties getLen, getWidth;
. 3, which defines a return getSize two properties get method, it returns a tuple by the length and composition of the broadband value, and to output a track execution trace method traces;
4, defines a self .__ length, self .__ width set methods setLen, setWidth, in order to track and process execution trace output traces;
5, defines two attributes deletion method delSize
. 6, respectively, using the relevant operation property bound size, len, width attribute, size supports get, set, and del, other two attributes are not supported del

size = property(getSize,setSize,delSize,'用元组存储的长方形的长和宽') 
len = property(getLen,setLen,0,'长方形的长') #虽然执行效果一样,但参数中的0最好用None
width = property(getWidth,setWidth,0,'长方形的宽')


Second, the case of the code (step into interactive mode)

class Rectangle():
   def __init__(self,length,width): self.__width,self.__length = width,length
   def setSize(self,size):
       print("execute setSize")
       self.__length,self.__width=size #使用序列解包获取长度和宽度
   def getSize(self):
       print("execute getSize ")
       return self.__length,self.__width
   def delSize(self):
       print("execute delSize ")
       self.setSize((0,0))
   def setLen(self,length):
       print("execute setLen")
       self.__length=length
   def getLen(self):
       print("execute getLen")
       return self.__length
   def setWidth(self,width):
       print("execute setWidth")
       self.__width=width
   def getWidth(self):
       print("execute getWidth")
       return self.__width
   
   size = property(getSize,setSize,delSize,'用元组存储的长方形的长和宽')
   len = property(getLen,setLen,0,'长方形的长')
   width = property(getWidth,setWidth,0,'长方形的宽')
   
rect = Rectangle(5,3)
rect.size,rect.len,rect.width
rect.size=(5,4)
rect.size,rect.len,rect.width
help(Rectangle.size)
print(Rectangle.len.__doc__,Rectangle.width.__doc__,sep='\n')
del rect.size


Third, the case screenshots
  


    You can clearly see from the top of the track, all access to the property is defined attributes are called access methods corresponding assignment is not a direct assignment, but call setSize, setLen, setWidth method, del not directly deleted, but the call delSize method, the query variable is the get method call.
This section details the case binding process in detail using the property function of the instance attribute in a class definition for a simple external access, can clearly see the properties of the property will be defined by the method of read and write access by the inner case, and may be between the interior and the attribute many relationship instance variables, instance variables and even relationship does not direct execution method (e.g., the above method of instance variables associated access statement is removed leaving only the print statement is such an effect) .
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/93350685