class property methods getting called only once

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jm.suresh@no.spam.gmail.com

    #1

    class property methods getting called only once

    In the following code, I could not find out why the set and get methods
    are not called once I set the property.

    >>class Test:
    .... def __init__(self):
    .... self._color = 12
    .... def _setcolor(self, value):
    .... print 'setting'
    .... self._color = value
    .... def _getcolor(self) :
    .... print 'getting'
    .... return self._color
    .... color = property(_getco lor,_setcolor)
    ....
    >>a = Test()
    >>a.color
    getting
    12
    >>a.color = 22
    >>a.color
    22

    For some reason the set method is not getting called at all, Anybody
    has any clue?

    thanks.
    --
    Suresh

  • limodou

    #2
    Re: class property methods getting called only once

    >On 3 Dec 2006 21:24:03 -0800, jm.suresh@no.sp am.gmail.com
    <jm.suresh@gmai l.comwrote:
    In the following code, I could not find out why the set and get methods
    are not called once I set the property.
    >
    >
    >class Test:
    ... def __init__(self):
    ... self._color = 12
    ... def _setcolor(self, value):
    ... print 'setting'
    ... self._color = value
    ... def _getcolor(self) :
    ... print 'getting'
    ... return self._color
    ... color = property(_getco lor,_setcolor)
    ...
    >a = Test()
    >a.color
    getting
    12
    >a.color = 22
    >a.color
    22
    >
    For some reason the set method is not getting called at all, Anybody
    has any clue?
    >
    thanks.
    property can only be used in New Style Class. So you should write your Test as:

    class Test(object):

    And try again.

    --
    I like python!
    UliPad <<The Python Editor>>: http://wiki.woodpecker.org.cn/moin/UliPad
    My Blog: http://www.donews.net/limodou

    Comment

    Working...