Question

I am looking at this Python Doc page:

http://docs.python.org/2/library/functions.html#property

class C(object):
    def __init__(self):
        self._x = None

    def getx(self):
        return self._x
    def setx(self, value):
        self._x = value
    def delx(self):
        del self._x
    x = property(getx, setx, delx, "I'm the 'x' property.")

Right below it says:

If then c is an instance of C, c.x will invoke the getter, c.x = value will invoke the setter and del c.x the deleter.

To me, c.x = value looks like assignment of a value to a function, since c.x is a function, unless the "=" operator is overloaded. Is it what is happening here?

Same thing with del c.x

Thanks.

Was it helpful?

Solution

property is a descriptor, which changes the way Python handles attribute access. The Python docs have an article introducing descriptors.

When Python accesses an attribute that points to an object with a __get__ method, it will return what that method returns instead of the object itself. Similarly, = will delegate to __set__ and del to __delete__. The special methods are described in the docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top