Last Updated: February 25, 2016
·
572
· geeknam

Python descriptors

class property(object):

    def __init__(self, getter, name=None):
        pass

    def __get__(self, instance, owner):
        # Because 42 is the answer to all the questions
        return 42



class Rectangle(object):

    def __init__(self, x, y):
        self.x = x
        self.y = x

    @property
    def area(self):
        return self.x * self.y


r = Rectangle(10, 10)
print r.area  # the result is 42