Better Programming

Advice for programmers.

Follow publication

Member-only story

@property in Python—What Is It and How Do You Use It?

Learn how to create getters and setters in Python

Artturi Jalli
Better Programming
Published in
4 min readJul 9, 2021
Crystal ball in the dark
Photo by Michael Dziedzic on Unsplash.

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 func
def some_func():
pass
some_func = extend(some_func)

It extends the behavior of some_func() through the behavior of the extend() function.

Learn more about decorators:

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

No responses yet

Write a response