Esmail wrote:
I am just reading about properties in Python. I am thinking
of this as an indirection mechanism, is that wrong? If so, how
come the getter/setters aren't called when I use properties
instead of the functions directly?
Because you weren't actually using them.  You were writing:
    size = 3, 7
not:
    r.size = 3, 7
Try:
    class Rectangle(object):
        def __init__(self):
            self._width = 0 # use single underscore unless you
            self._height = 0 # have a serious reason for __x
        def setSize(self, shape): # a setter takes a single arg
            width, height = shape
            print 'in setter'
            if width < 0 or height < 0:
                print >> sys.stderr, 'Negative values not allowed'
            else:
                self._width = width
                self._height = height
        def getSize(self):
            print 'in getter'
            return self._width, self._height
        size = property(getSize, setSize)

    r = Rectangle()
    print r.getSize()
    r.setSize((-40, 30))
    print r.getSize()
    print '-----------------'
    r.size = 3, 7
    print r.size
    print '-----------------'
    r.size = -3,7
    print r.size

--Scott David Daniels
scott.dani...@acm.org
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to