程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Usage of @property property in Python

編輯:Python

Catalog

One 、 Preface

Two 、 Create properties for calculation

3、 ... and 、 Add security to properties

One 、 Preface

The 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 calculation

stay 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 properties

stay 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 !



  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved