understanding 'property' / making value read-only

Achim Domma achim.domma at syynx.de
Sun Dec 29 07:30:42 EST 2002


Hi,

I wrote the following example:

class Demo:
    def __init__(self,value):
        self._value = value

    def _getValue(self):
        print "_getValue"
        return self._value

    value = property(_getValue,None,None,"DocString")

a = Demo(10)
print a.value
a.value = 15
print a.value


and the output is:


_getValue
10
15


Why can I still set 'value'? Is it possible at all to make it read-only or
do I have to write a funktion _setValue(self,value) which raises an
exception?

regards,
Achim





More information about the Python-list mailing list