![]() |
VOOZH | about |
property() function in Python is a built-in function that returns an object of the property class. It allows developers to create properties within a class, providing a way to control access to an attribute by defining getter, setter and deleter methods. This enhances encapsulation and ensures better control over class attributes. Example:
shakshi
Explanation: Student class initializes _name in __init__. The property() function with a lambda defines a read-only name property, restricting modifications. print(s.name) accesses and prints _name.
property(fget=None, fset=None, fdel=None, doc=None)
Here none is the default value if a getter, setter or deleter is not provided.
Parameters:
Return: Returns a property object from the given getter, setter and deleter.
Note:
- 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.
We can create properties using two methods:
Getting value GeeksforGeeks Setting value to GfG Deleting value
Explanation: Alphabet class uses the property function to manage a private attribute _value with getter, setter and deleter methods. The __init__ method initializes _value. getValue retrieves and prints it, setValue updates and prints the change and delValue deletes it with a message. Accessing x.value calls getValue, assigning a new value calls setValue and deleting it calls delValue.
Getting value Peter Setting value to Diesel Deleting value
Explanation: Alphabet class uses the @property decorator to manage _value with getter, setter and deleter methods. The getter prints "Getting value" and returns _value, the setter updates it with a message and the deleter removes it while printing "Deleting value." Creating an instance with "Peter" triggers the getter on print(x.value), setting "Diesel" calls the setter and del x.value invokes the deleter.
Understanding the difference between properties and attributes in Python is important for writing clean, maintainable code. Properties provide controlled access to instance attributes, while attributes are direct data members of a class or instance.
A property allows encapsulation by defining custom logic for getting, setting and deleting values. This helps in implementing validation and computed attributes dynamically. In contrast, class attributes are shared among all instances of a class and are typically used for static data that should not be changed on an instance level.
Example 1. class attributes vs Instances attribute
1 2 2
Explanation: Employee class has a shared class attribute count, initialized to 0. The increase method increments count. Calling increase() on emp1 sets count to 1 and on emp2, it updates to 2. Printing emp1.count, emp2.count and Employee.count all return 2.
Example 2. Using property() for encapsulation
Getting value Happy Coding! Setting value to Hey Coder! Deleting value
Explanation: GFG class uses property to manage _value with getter, setter and deleter methods. The getter retrieves _value, the setter updates it and the deleter removes it, each with a message. Creating an instance with "Happy Coding!", print(x.value) calls the getter, assigning "Hey Coder!" updates _value and del x.value deletes it.
Feature | class attribute | property() |
|---|---|---|
Definition | Shared across all instances of the class. | Manages instance attributes with getter, setter, and deleter. |
Modification | Changes affect all instances. | Controls access to instance attributes. |
Encapsulation | No encapsulation, direct modification. | Provides controlled access using methods. |
Usage | Used for static values across instances. | Used for dynamic control over instance attributes. |