Property (@property)

say it: “PROP-er-tee” /ˈprɑpərti/

Lets a method be used like an attribute, with no brackets.

What does @property do?

@property lets you use a method like an attribute, with no brackets. It's used for values worked out on the fly, or to check a value before it's saved.

class Cart:
    def __init__(self, prices):
        self.prices = prices

    @property
    def total(self):
        return sum(self.prices)

cart = Cart([5, 10])
print(cart.total)  # 15, no brackets

Level: Freelancer · More Python terms at this level

Updated