Logo

Python Properties: Stop Touching My Attributes Directly!

Ever wanted more control over how your object's attributes are accessed? Python's `@property` decorator is your answer! Think of it as a gatekeeper for your object's data.

Essentially, `@property` lets you define methods that act like attributes. Instead of directly accessing `my_object.attribute`, you can access it via `my_object.attribute` but *behind the scenes*, Python calls a method.

This unlocks powerful possibilities. You can add validation logic before setting a value, perform calculations on retrieval, or even make an attribute read-only. For example:

```python
class Circle:
def __init__(self, radius):
self._radius = radius # Note the underscore! Suggests internal use only.

@property
def radius(self):
return self._radius

@radius.setter
def radius(self, value):
if value <= 0:
raise ValueError("Radius must be positive")
self._radius = value

@property
def area(self):
return 3.14159 * self._radius ** 2 # Calculate on the fly!
```

`@property` allows us to expose `radius` as a regular attribute while still controlling its behavior and even adding the `area` property calculated each time it's called. Start using `@property` to write cleaner, more robust, and maintainable Python code!

See all content

Subscribe now and never miss an update!

Subscribe to receive weekly news and the latest tech trends

nerdy-mind 2025. All rights reserved