The usage and meaning of python @ property are fully parsed, and @ property is fully parsed.
The code that you first came into contact with python is to take the length and width of the rectangle, define a rectangle class, set the length and width attributes of the rectangle, and call the length and width Through instantiation, as shown in the following code.
class Rectangle(object): def __init__(self): self.width =10 self.height=20r=Rectangle()print(r.width,r.height)
The output result is 10 20.
However, this will cause a serious problem in actual use. The attributes defined in __init _ are variable. In other words, all developers who use a system can modify the property name (although it may be unconscious) at will ), however, this can easily cause serious consequences.
class Rectangle(object): def __init__(self): self.width =10 self.height=20r=Rectangle()print(r.width,r.height)r.width=1.0print(r.width,r.height)
The above Code results will output 1.0 in width. It may be that the developer accidentally clicked a decimal point, but the system data may be incorrect and it is difficult to troubleshoot in some cases.
This is a very reluctant situation in production. At this time, we will consider whether the width attribute can be set to private. Other people cannot change the attribute at will. If you want to change the attribute, you can only modify it according to my method, @ property plays this role (similar to private in java)
Class Rectangle (object): @ property def width (self): # The variable name is not the same as the method name and is changed to true_width, the same as return self. true_width @ property def height (self): return self. true_heights = Rectangle () # Same as method name s. width = 1024s. height = 768 print (s. width, s. height)
(@ Property calls a method like a property, just like a special property)
In this case, if you want to assign a value to width again, the AttributeError: can't set attribute error will be reported. This ensures the security of the attribute.
In addition, in order to solve the attribute operation, an encapsulation method is provided to modify the attribute.
Class Rectangle (object): @ property def width (self): # The variable name is not the same as the method name and is changed to true_width, the same as return self. true_width @ width. setter def width (self, input_width): self. true_width = input_width @ property def height (self): return self. true_height @ height. setter # The method name defined by property must be the same as def height (self, input_height): self. true_height = input_heights = Rectangle () # Same as method name s. width = 1024s. height = 768 print (s. width, s. height)
In this case, you can assign values to "attributes". The same method is del, which is used to delete attributes. The syntax is as follows. The specific implementation is not described in detail.
@height.deleterdef height(self): del self.true_height
To sum up@ Property provides readable, writable, and deletable operations. If it is similar to read-only operations, you only need to define @ property. If it is not defined, other operations are prohibited.
The usage and meaning of the above python @ property are all the content shared by the editor. I hope you can give us a reference and support the house of helpers.