One 、 Preface
Two 、 Create properties for calculation
3、 ... and 、 Add security to properties
One 、 PrefaceThe properties introduced in this article are different from class properties and instance properties . The properties introduced by class properties and instance properties will return the stored values . The attribute to be introduced in this article is a special attribute , Its value will be calculated when it is accessed . in addition , This property can also add a security mechanism for the property .
Two 、 Create properties for calculationstay Python in , Can pass @property( Decorator ) Convert a method to an attribute , So as to realize the properties for calculation . After converting a method to an attribute , You can access the method directly through the method name , Instead of adding a pair of braces “()”, This makes the code simpler .
adopt @property The syntax format for creating properties for computation is as follows :
@propertydef methodname(self):block
Parameter description :
methodname: Used to specify the method name , It usually starts with a lowercase letter . The name will end up as the name of the created property .
self: Necessary parameters , Represents an instance of a class .
block: Method body , Specific functions realized . In the method body , Usually, the return End of sentence , Used to return calculation results .
for example , Define a rectangular class , stay __init__() Method to define two instance properties , Then define a method to calculate the rectangular area , And Application @property Convert it to an attribute , Finally, create an instance of the class , And access the converted properties
The code is as follows :
class Rect: def __init__(self, width, height): self.width = width # The width of the rectangle self.height = height # The height of the rectangle @property # Convert methods to properties def area(self): return self.width * self.height # Returns the area of a rectangle rect = Rect(800, 600) # Create examples print(" Area is :", rect.area) # Output attribute value
Run the above code , The following run results will be displayed :
3、 ... and 、 Add security to propertiesstay Python in , By default , Create class properties or instances , It can be modified outside the class , If you want to limit it, you can't modify it outside the class , You can make it private , But when set to private , You can't get its value outside the class . If you want to create one that can read , But properties that cannot be modified , Then you can use @property The implementation only reads properties .
for example , Create a TV program class TVshow, Create another show attribute , Used to display the currently playing TV program , The code is as follows :
class TVshow: # Define TV program classes def __init__(self, show): self.__show = show @property # Convert methods to properties def show(self): # Definition show() Method return self.__show # Return private property value tvshow = TVshow(" Playing 《 Warwolf 》") # Create an instance of a class print(" Default :", tvshow.show) # Get attribute value
Run the above code , The following run results will be displayed :
Created by the above method show Properties are read-only , Try to modify the attribute value , Get back to . Add the following code under the method of the above code :
tvshow.show = " Playing 《 The red sea action 》"print(" After modification :", tvshow.show)
After operation , The running results as shown in the figure below will be displayed , The exception information of the scarlet letter is to modify the attribute show Exception thrown when .
adopt @ You can not only set the property to read-only , And you can set interceptors for properties , That is, it is allowed to modify the properties , However, certain constraints need to be observed when modifying .
This is about python in @Property This is the end of the article on using attributes , More about python @Property Please search the previous articles of software development network or continue to browse the relevant articles below. I hope you will support software development network more in the future !