We know Python syntax for using the property function is defined when the property access methods:
Examples of attributes = property (fget = None, fset = None, fdel = None, doc = None)
But to @property decorator can also achieve the same function, but @property decorator requirements must be defined getter decorators to achieve get property access method, and property documentation for this function is not required, only requires fget, fset, fdel there can be a valid. So what happens if you do not define fget? Let's look at a simple example:
>>> 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
len = property(None,setLen,None,'长方形的长')
>>> rect=Rectangle(10,5)
>>> rect.len
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
rect.len
AttributeError: unreadable attribute
>>> rect.len=15
execute setLen
>>>
You can see from the above code, fget can not set, but will be reported when performing attribute read attribute can not be read.
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!