
Property in Python
Property in Python With Python’s `property()`, you can create managed attributes in your classes. These managed attributes, also known as properties, are useful when you need to modify their internal implementation without changing the public API of the class. Properties are a popular way to create managed attributes quickly in a Pythonic style.
In this article, we’ll cover the following:
- Creating managed attributes or properties in your classes
- Performing lazy attribute evaluation and providing computed attributes
- Avoiding setter and getter methods to make your classes more Pythonic
- Creating read-only, read-write, and write-only properties
- Creating consistent and backwards-compatible APIs for your classes
Let’s dive into each aspect with Python code examples to illustrate their usage.
Creating Managed Attributes in Your Classes
When creating managed attributes with property(), you can use decorators to define getter, setter, and deleter methods for the attribute.
class Temperature:
def __init__(self, celsius):
self._celsius = celsius
@property
def fahrenheit(self):
return self._celsius * 9 / 5 + 32
@fahrenheit.setter
def fahrenheit(self, value):
self._celsius = (value - 32) * 5 / 9In the above example, the @property decorator marks the fahrenheit method as a property, and @fahrenheit.setter decorates the method which allows setting the value of the property.
Performing Lazy Attribute Evaluation and Providing Computed Attributes
You can use property() to perform lazy attribute evaluation and provide computed attributes.
class Circle:
def __init__(self, radius):
self._radius = radius
self._area = None
@property
def area(self):
if self._area is None:
print("Computing area")
self._area = 3.14 * self._radius ** 2
return self._areaIn this example, the area property is computed only when it's accessed for the first time.
Creating Read-Only, Read-Write, and Write-Only Properties
You can create properties with different access levels, such as read-only, read-write, and write-only.
class Square:
def __init__(self, side_length):
self._side_length = side_length
@property
def side_length(self):
return self._side_length
@side_length.setter
def side_length(self, value):
if value <= 0:
raise ValueError("Side length must be positive")
self._side_length = value
@property
def area(self):
return self._side_length ** 2In the above example, the side_length property has both getter and setter methods, while the area property is read-only.
Backwards-Compatible APIs for Your Classes
Using property(), you can create consistent and backwards-compatible APIs for your classes.
class User:
def __init__(self, username):
self._username = username
@property
def username(self):
return self._username
@username.setter
def username(self, value):
if not value.isalnum():
raise ValueError("Username must be alphanumeric")
self._username = valueIn this example, the username property can be modified in a controlled manner, ensuring that any changes to the property adhere to the specified rules.
By using property(), you can create managed attributes, perform lazy attribute evaluation, provide computed attributes, and create consistent APIs for your classes in a Pythonic style.
If you are interested in further exploring this topic, you can find additional resources and examples in the Python’s official documentation.
By leveraging the power of property(), you can enhance the flexibility and robustness of your classes in Python.






