Propiedades

Las propiedades permiten acceder a atributos ficticios por medio de métodos especiales:


In [2]:
class A(object):
    def __init__(self):
        self._size = 0

    @property
    def size(self):
        print("getting the property")
        return self._size

    @size.setter
    def size(self, value):
        print("setting the property")
        self._size = value
        
a = A()
print(a.size)
a.size = 20
print(a.size)


getting the property
0
setting the property
getting the property
20