Sick problem with Python properties

Fredrik Lundh fredrik at pythonware.com
Thu Aug 7 07:48:46 EDT 2003


webmaster at t-dose.de wrote:
> See the following code. I wonder why in this case neither
> setp() nor getp() is called !?
>
> yetix@/home/ajung(1)% cat test.py
> class A:
>
>     def __init__(self):
>         self._p=None
>
>     def setp(self, p):
>         print 'set'
>         self._p = p*p
>
>     def getp(self):
>         print 'get'
>         return self._p
>
>     pp = property(getp, setp)
>
>
> inst=A()
> inst.p = 9
> print inst.p
>
> yetix@/home/ajung(2)% python2.3 test.py
> 9

you need to 1) inherit from the "object" base class, and 2) change
"inst.p" to "inst.pp" in the example.

</F>








More information about the Python-list mailing list