Image generated by Midjourney

And 5 ways to use it in data science and machine learning

@property is my favorite decorator in Python. I have been using Python for many years now, and with each passing year, my expertise and comfort level with the language gradually grows. Among all the techniques and tricks that I’ve learned to write better code over the years, the property decorator stands out as the most remarkable one. It has consistently helped me tackle complex problems and write clean, efficient, and elegant code.

The @property decorator is an incredibly powerful feature that enhances the way you can interact with your class attributes. It can be a powerful way to intertwine attributes and create dependencies among them. In this beginner-friendly guide, we’ll explore the underlying concept of properties and dive into 5 different ways you can use them to supercharge your classes. We’ll also walk through some practical examples in data science and machine learning to solidify your understanding.

Side note: I get so excited when I get to introduce properties to a Python enthusiast for the first time and see sparkles in their eyes. If this is your first time learning about them, I hope you get sparkles in your eyes too! If you’re already a seasoned developer and familiar with the concept, I hope you still find something new in this post. In any case, please let me know in the comments if there is anything that could have been explained better or if there are any other important use cases I may have missed.

TL;DR, in Python, a property is a special attribute that allows controlled access to certain aspects of an object. Consider this simple class that defines a Student with two simple attributes name, and class. we can instantiate and access the name attribute as follows: