DEV Community

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

Posted on

4 1

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

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay