FAQ
Hi,

I have a problem with setting a property to a class instance, in
python 2.5.1. The property is defined through get and set methods, but
when I set it, the setter doesn't get called. Instead, I believe the
property in the instance gets replaced with a new object (string).

I have the following code (stripped down from my program):

class Model():
def __init__(self, title = ''):
self._views, self._title = [], None
self.setTitle(title)

def addView(self, view):
self._views.append(view)
view.onModelUpdated()

def updateViews(self, trigger = None):
for view in self._views:
view.onModelUpdated(trigger)

def getTitle(self):
return self._title
def setTitle(self, t):
#if self.debug:
if self._title != t:
self._title = t
self.updateViews()
title = property(fget=getTitle, fset=setTitle)

# client code:

class CountUpdatesView():
def __init__(self):
self.updates = 0

def onModelUpdated(self, trigger):
self.updates += 1


m = Model('test')
v = CountUpdatesView()
m.addView(v) # addView calls into v.onModelUpdated() and
# v.updates will be 1 after this
m.title = 'new title' # Model.setTitle calls into v.onModelUpdated
()
# and v.updates should become 2
print m._title # should print 'new title'; instead, prints
'test'

When assigning to m.title, v.updates is not changed, and the last line
prints the original value, passed to the constructor.
Am I missing something?

I noticed that if I put m.setTitle('new title') in the client code,
the code works correctly.

Search Discussions

  • Christian Heimes at Feb 26, 2009 at 7:56 am

    Dan Barbus schrieb:
    Hi,

    I have a problem with setting a property to a class instance, in
    python 2.5.1. The property is defined through get and set methods, but
    when I set it, the setter doesn't get called. Instead, I believe the
    property in the instance gets replaced with a new object (string).

    I have the following code (stripped down from my program):

    class Model():
    def __init__(self, title = ''):
    self._views, self._title = [], None
    self.setTitle(title)
    *snip*

    Properties don't work correctly on old style classes. You have to
    subclass from object in order to get a new style class.

    class Model(object):
    pass

    Christian
  • Utnapistim at Feb 26, 2009 at 2:04 pm

    On Feb 26, 9:56?am, Christian Heimes wrote:
    Dan Barbus schrieb:
    Hi,
    I have a problem with setting a property to a class instance, in
    python 2.5.1. The property is defined through get and set methods, but
    when I set it, the setter doesn't get called. Instead, I believe the
    property in the instance gets replaced with a new object (string).
    I have the following code (stripped down from my program):
    class Model():
    ? ? def __init__(self, title = ''):
    ? ? ? ? self._views, self._title = [], None
    ? ? ? ? self.setTitle(title)
    *snip*

    Properties don't work correctly on old style classes. You have to
    subclass from object in order to get a new style class.

    class Model(object):
    ? ? pass

    Christian
    Thanks, that solved the problem.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 26, '09 at 7:46a
activeFeb 26, '09 at 2:04p
posts3
users2
websitepython.org

2 users in discussion

Utnapistim: 2 posts Christian Heimes: 1 post

People

Translate

site design / logo © 2022 Grokbase