understanding 'property' / making value read-only

Gonçalo Rodrigues op73418 at mail.telepac.pt
Sun Dec 29 07:59:19 EST 2002


On Sun, 29 Dec 2002 13:30:42 +0100, "Achim Domma" <achim.domma at syynx.de>
wrote:

>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?

You have to make Demo a subclass of some new-style class for properties
to work right. Old-style classes do not go well with properties, e.g.
something like

class Demos(object):
    <whatever> 

is enough.

>
>regards,
>Achim
>

With my best regards,
G. Rodrigues



More information about the Python-list mailing list