Python class @property

The @property method in Python class can be accessed as a property or attribute.

class Triangle:

    def __init__(self, width, height):
        self.width = width
        self.height = height

    @property
    def area(self):
        return self.width * self.height / 2


t = Triangle(4, 6)
area = t.area

print(area)  # 12.0

The triangle object can access area regardless of being a method.

area() -> area

Comments

Powered by Markdown

More