Member-only story
@property in Python—What Is It and How Do You Use It?
Learn how to create getters and setters in Python
The @property
in Python is a property decorator. It makes it possible to access an object’s method like a regular attribute:
mass.pounds() ---> mass.pounds
You can use property decorators to give a method of a class the getter, setter, and deleter functionality.
Decorators in a Nutshell
Before diving deeper into Python’s property decorators, let’s take a look at what decorators are.
In Python, decorators are marked with @
.
A decorator is a way to extend the behavior of a function or a class.
In short, this piece of code:
def extend(func):
return func@extend
def some_func():
pass
Does the same as this:
def extend(func):
return funcdef some_func():
passsome_func = extend(some_func)
It extends the behavior of some_func()
through the behavior of the extend()
function.
Learn more about decorators: