This article example describes Python class, property property (simplify the operation of properties), @property, property() usage. Shared for your reference, as follows:
There are two ways to create property attributes: 1. @property decorator approach 2. class attribute approach (class attribute = property())
The property attribute can simplify the operation of the instance object on the property (get, set), and can do type-checking and preprocessing on the property, etc.
Decorator approach:
(@property, getting the value of a property, the way both old-style and new-style classes do it):
class Goods: @property # Can only be passed the self argument. Must return a value def size(self): return 100 # Must return a value obj = Goods() result = # call attributes (without the parentheses, call the function in the same way as an attribute) print(result) # property property generally does some preprocessing or formatting of the property value, etc., which can simplify the acquisition of the property.
Run results:
100
(@,@,set attribute values, delete attributes, the way newer classes support):
class Goods(object): def __init__(self): # Original price self.original_price = 100 # Discounts = 0.8 @property # Way 1 gets the value of the attribute (supported by both newer and older classes) def price(self): # Actual price = Original price * Discount new_price = self.original_price * return new_price @ # Mode 2 Setting the value of an attribute (not supported by old-style classes, requires two parameters to be passed) def price(self, value): self.original_price = value @ # Mode 3 Delete Attributes (not supported by legacy classes) def price(self): del self.original_price obj = Goods() # Get the price of the item (automatically calls the @property modifier function) = 200 # Modify the original price of the product (automatically calls the @-modified function and passes 200 to the function) del # Delete the original price of the product (automatically calls the @-modified function)
Class attribute approach:
(Class attributes create property attributes, supported by both old-style and new-style classes):
class Goods(object): def __init__(self): # Original price self.original_price = 100 # Discounts = 0.8 def get_price(self): # Actual price = Original price * Discount new_price = self.original_price * return new_price def set_price(self, value): self.original_price = value def del_price(self): del self.original_price # Class properties. property(methodname1,methodname2,methodname3, "property description information") PRICE = property(get_price, set_price, del_price, "Attribute description information...") obj = Goods() # Get the price of the item Automatically calls the method specified by the first argument of the property() function. = 200 # Set the original price of the item Automatically calls the method specified by the second argument of the property() function. desc = .__doc__ # Automatically get the value set in the fourth parameter: attribute description information... print(desc) del # Delete the original price of the item Automatically calls the method specified by the third argument of the property() function.
Readers interested in more Python related content can check out this site's topic: thePython Object-Oriented Programming Introductory and Advanced Tutorials》、《Python Data Structures and Algorithms Tutorial》、《Summary of Python function usage tips》、《Summary of Python string manipulation techniques》、《Summary of Python coding manipulation techniquesand thePython introductory and advanced classic tutorials》
I hope the description of this article will be helpful for you Python programming.