In Python, the main purpose of
Property() function is to create property of a class.
Syntax: property(fget, fset, fdel, doc)
fget() – used to get the value of attribute
fset() – used to set the value of attribute
fdel() – used to delete the attribute value
doc() – string that contains the documentation (docstring) for the attribute
Return: Returns a property attribute from the given getter, setter and deleter.
- If no arguments are given, property() method returns a base property attribute that doesn’t contain any getter, setter or deleter.
- If doc isn’t provided, property() method takes the docstring of the getter function.
Example #1: Using property() method
Getting value GeeksforGeeks Setting value to GfG Deleting value
The main work of decorators is they are used to add functionality to the existing code. Also called metaprogramming, as a part of the program tries to modify another part of the program at compile time.
Example #2: Using @property decorator
Getting value Peter Setting value to Diesel Deleting value
Using @property decorator works same as property() method.
First, specify that value() method is also an attribute of Alphabet then, we use the attribute value to specify the setter and the deleter. Notice that the same method value() is used with different definitions for defining the getter, setter and deleter. Whenever we use x.value, it internally calls the appropriate getter, setter and deleter.
By using property() method, we can modify our class and implement the value constraint without any change required to the client code. So that the implementation is backward compatible.
- Python - Call function from another function
- Returning a function from a function - Python
- join() function in Python
- Python reversed() function
- Python | fsum() function
- Python | tuple() Function
- Difference between Method and Function in Python
- Python | __import__() function
- Help function in Python
- Python | locals() function
- Currying Function in Python
- Python | datetime.timedelta() function
- time.process_time() function in Python
- time.perf_counter() function in Python
- time.strftime() function in Python
- Internal Working of the len() Function in Python
- Function Composition in Python
- Python - Get Function Signature
- Python min() Function
- How to find the number of arguments in a Python function?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : shubham_singh