[PyQt] dip @setter bug?

Phil Thompson phil at riverbankcomputing.com
Fri Jun 15 21:27:01 BST 2012


On Fri, 15 Jun 2012 19:21:42 +0100, Robert Kent <rob at gulon.co.uk> wrote:
> Hi,
> 
> I'm just starting to play with dip as I think the idea is very cool. I
> have however either missed something (likely) or found a bug (unlikely).
> Running the following code:
> 
> ############################
> ############################
> from dip.model import Model, Int, Str
> 
> class ExampleModel(Model):
>     name=Str()
>     address=Str()
>     age=Int(30)
> 
>     @name.default
>     def name(self): return 'Default'
> 
>     @name.setter
>     def name(self, name):
>         self._name=name.upper()
>         print "Set:", self._name
> 
>     @name.getter
>     def name(self):
>         return self._name
> 
> model=ExampleModel()
> print "Name:", model.name
> print "Address:", model.address
> print "Age:", model.age
> 
> model.name="bill"
> print "Name:", model.name
> 
> ############################
> ############################
> 
> I get the following output:
> 
> Name: Default
> Address: 
> Age: 30
> Set: BILL
> Name: bill
> 
> I would expect the 'Name: bill' line to read 'Name: BILL' as proved by
the
> print in the decorator… why doesn't it? Is the default setter being
called
> subsequently to my decorated function for some reason?
> 
> Thanks very much in advance,

_name is the "shadow" value of name. In the current version (it wasn't
always the case) the shadow value is always set (even if there is an
explicit setter) and it is set after calling the setter (so that the setter
has access to the old value).

What's happening, therefore, is that your setting of _name is being
overwritten when your setter returns. Using _something_else instead of
_name in your code will give you the behaviour you are expecting.

Phil


More information about the PyQt mailing list