Understanding properties

Graham Ashton graham at effectif.com
Sat Jul 6 12:27:23 EDT 2002


I clearly don't get it. I just tried a simple test:

class MyObject:
    def __init__(self):
        self.__size = 0
    def getSize(self):
        print "getSize() called"
        return self.__size
    def setSize(self, s):
        print "setSize(%s) called" % size
        self.__size = s

    size = property(getSize, setSize, None, 'Size of instance')

if __name__ == '__main__':
    o = MyObject()
    size = o.size
    print "original size:", size
    o.size = 10
    size = o.size
    print "new size:", size

When I run it I get:

% python MyStuff.py
getSize() called
original size: 0
new size: 10

I was expecting to see several calls to getSize() and a call to setSize(),
but I think when I do "o.size = 10" the setSize() method (as set by the
property) isn't kicking in.

What am I missing? Thanks.

--
Graham



More information about the Python-list mailing list