Python property() Function
Description
property()The function is used to return attribute values in new-style classes.
Syntax
The following is the syntax of the property() method:
class property([fget[, fset[, fdel[, doc]]]])
Parameters
- fget -- Function to get the attribute value
- fset -- Function to set the attribute value
- fdel -- Function to delete the attribute value
- doc -- Attribute description information
Return Value
Returns new-style class attributes.
Examples
Define a controllable attribute value x
IfcYesCIf c is an instance of C, c.x will trigger the getter, c.x = value will trigger the setter, and del c.x will trigger the deleter.
If the doc parameter is given, it will become the docstring of this attribute value; otherwise, the property function will copy the docstring of the fget function (if any).
Using the property function as a decorator makes it convenient to create read-only properties:
The code above converts the voltage() method into a getter method for a read-only property of the same name.
The getter, setter, and deleter methods of property can also be used as decorators:
This code is exactly the same as the first example, but note that the names of these additional functions are the same as those under property, such as x here.
Other Extensions
Python Built-in Functions