Forem

Cover image for Decorators & Class Property in Python
Elchonon Klafter
Elchonon Klafter

Posted on

4

Decorators & Class Property in Python

In this post I will discuss the common usage of function decorators, specifically the class property decorator as well as the difference between using the decorator or the class method directly.

Usage

Code snippet of class using @property decoratorLooks familiar, doesn't it?
The ubiquitous @propery decorator is found everywhere, but how does it actually work?
What are the differences between using @property or using the property() class method directly?

Code snippet of class using property method

How do decorators even work?

Here's an example of a standard decorator function.
decorator function example
Under the Python hood, the function that calls the decorator (ie. the outer function) gets wrapped with additional code by calling the decorator and passing the outer function as a parameter. The decorator will create a wrapped function with all it's new logic and return it as the value of the outer function.

Understanding the property method

The class property method is a built-in Python function that that allows us to define getter, setter and deleter methods for class attributes, and binds them to the class default magic methods.

def get_my_value(self):
    return self._my_value
my_value = property(get_my_value)
Enter fullscreen mode Exit fullscreen mode

is understood by the Python compiler as

def __get__(self,obj):
    return self.fget(obj)
Enter fullscreen mode Exit fullscreen mode

Essentially, the property() method creates an object that acts as a proxy between the attribute name and the underlying data. It therefore allows us to add custom behavior and validation logic that wouldn't be possible with the default magic methods.

The @property Decorator

As we see above, the property method allows us to overwrite the default magic method and extent its functionality. We've also demonstrated how a decorator wraps functional logic within a new function. When a getter, setter or deleter method is called with the @property decorator, Python translates it into a call to the property() method internally, and creates a property object that wraps the method to a function that can overwrite the default magic methods.

Decorator Benefits

While we have demonstrated that using the @property decorator or property() method are equivalent, the decorator offers a simpler use, and allows the code within the class to be more readable. It is therefore considered best practice and coding convention and the decorator should be used unless there is a specific scenario requirement.

Credits:

https://www.geeksforgeeks.org/python-property-decorator-property/
Blog post header image generator
Code snippet maker

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more →

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

đź‘‹ Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay