Introduction: Python property() Function
Sometimes you need to control how a class attribute is read, updated, or deleted. This is common when validating data, calculating values, or protecting important information.
Writing separate methods for every attribute can make the code longer. The Python property() Function provides a cleaner way to handle this.
What it is: The property() function is a built-in Python function that creates a property object. It lets you access methods like normal attributes while still controlling how the value is read, changed, or deleted.
See a quick example below to understand how it works.
You can also check its real-world use cases to see where it is commonly used.
Now that you know the basics, let’s look at its syntax, parameters, and return value before moving to examples and use cases.
💡 Tip: The property() function helps keep your code clean by adding validation without changing how an attribute is accessed. To explore more built-in functions, visit the
Python Built-in Functions Learning Guide.
Syntax, Parameters, Return Value and Examples: Python property() Function
The following section explains the syntax, parameters, return value, and a quick example of the Python property() Function.
Syntax
property(fget=None, fset=None, fdel=None, doc=None)
Parameters
| Parameter | Description |
|---|---|
fget (optional) |
The function used to read the value. |
fset (optional) |
The function used to update the value. |
fdel (optional) |
The function used to delete the value. |
doc (optional) |
A documentation string for the property. |
Return Value
| Return Value | Description |
|---|---|
property |
Returns a property object. |
Quick Example
The following example creates a simple property.
class Student:
def __init__(self):
self._name = "Emma"
@property
def name(self):
return self._name
student = Student()
print(student.name)
# Output:
Emma
The @property decorator lets the method behave like an attribute. You can read name without calling it as a function.
How the Python property() Function Works
- The
property()function creates a property object. - It connects methods to an attribute.
- The getter returns the value.
- The setter updates the value.
- The deleter removes the value.
- The attribute can be used like a normal variable.
Examples: Python property() Function
The examples below show different ways to use the Python property() Function.
Example 1: Creating a Read-Only Property
class Student:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
student = Student("Emma")
print(student.name)
# Output:
Emma
Explanation: The @property decorator lets you read name like a normal attribute even though it is returned by a method.
Example 2: Updating a Value with a Setter
class Student:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
student = Student("Emma")
student.name = "Olivia"
print(student.name)
# Output:
Olivia
Explanation: The setter runs automatically when a new value is assigned. This allows the value to be updated without changing how the attribute is used.
Example 3: Validating Input
class Student:
def __init__(self):
self._age = 0
@property
def age(self):
return self._age
@age.setter
def age(self, value):
if value >= 0:
self._age = value
student = Student()
student.age = 18
print(student.age)
# Output:
18
Explanation: Before storing the value, the setter checks whether it is valid. This helps prevent incorrect data from being saved.
Example 4: Deleting a Property
class Student:
def __init__(self, name):
self._name = name
@property
def name(self):
return self._name
@name.deleter
def name(self):
del self._name
student = Student("Emma")
del student.name
print(hasattr(student, "_name"))
# Output:
False
Explanation: The deleter decides what happens when the property is deleted. In this example, the original attribute is removed.
Example 5: Calculating a Value
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
@property
def area(self):
return self.length * self.width
rectangle = Rectangle(8, 5)
print(rectangle.area)
# Output:
40
Explanation: The property calculates the area whenever it is accessed. No extra variable is needed to store the result.
Example 6: Using a Property Like a Normal Attribute
class Student:
def __init__(self):
self._marks = 90
@property
def marks(self):
return self._marks
student = Student()
print(student.marks)
# Output:
90
Explanation: Although marks is created by a method, it is used just like a normal attribute. This keeps the code simple to read.
Example 7: Preventing Invalid Data
class Product:
def __init__(self):
self._price = 0
@property
def price(self):
return self._price
@price.setter
def price(self, value):
if value > 0:
self._price = value
else:
print("Invalid price")
product = Product()
product.price = -100
# Output:
Invalid price
Explanation: The setter checks the value before saving it. Since the price is negative, the object keeps its previous value.
Use Cases: When to use the property() Function
The Python property() Function is useful in situations like these:
- Controlling access to object attributes.
- Validating values before saving them.
- Creating read-only attributes.
- Calculating values when they are needed.
- Keeping internal data protected.
- Writing cleaner object-oriented programs.
Key Takeaways: property() Function
Here are the main points to remember about the Python property() Function:
- The
property()function creates managed attributes. - A property can have a getter, setter, and deleter.
- Properties are used like normal attributes.
- They help validate data before storing it.
- They can return calculated values.
- They make classes easier to maintain.
The property() function helps control how attributes are read, updated, or deleted. It also makes your classes easier to use because methods can be accessed like normal attributes.