Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

understanding 'property' / making value read-only

2 views
Skip to first unread message

Achim Domma

unread,
Dec 29, 2002, 7:30:42 AM12/29/02
to
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


Gonçalo Rodrigues

unread,
Dec 29, 2002, 7:59:19 AM12/29/02
to
On Sun, 29 Dec 2002 13:30:42 +0100, "Achim Domma" <achim...@syynx.de>
wrote:

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

Achim Domma

unread,
Dec 29, 2002, 7:48:15 AM12/29/02
to
I just found it out myself: I have to use new-style classes, so deriving
Demo from object makes it work!


Laura Creighton

unread,
Dec 29, 2002, 7:06:04 PM12/29/02
to
Perhaps http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/59878
already does what you want.

Laura Creighton

0 new messages