properties with subscripted variables

Bruno Desthuilliers bruno.42.desthuilliers at websiteburo.invalid
Tue Aug 26 06:56:13 EDT 2008


Fabrizio Pollastri a écrit :
> Hi,
> I work on the python module AVC (http://avc.inrim.it) useful for the
> development of applications with GUIs. AVC is based on the property
> mechanism: any a variable controlled by AVC is set as a property, so
> when it is assigned by the application program, the __set__ function
> is called and AVC does its job. This works fine with non sequence
> types and with sequence types when are assigned as a whole, without
> subscripting. When the assignment has a subscript, the __set__ method
> is no more called.

Nope, but the __get__ method is.

> This is a limitation from the point of view of AVC.
> My goal would be to be able to intercept (trigger a call to a method
> of my module) any kind of variable assignment, even if it is a
> sequence type with a subscript.
> There is a way to reach this result with properties or with other
> ways?

The code:

   obj.prop[x] = y

is equivalent to:

   prop = obj.prop
   prop[x] = y

IOW, you just have to return from prop.__get__ a custom sequence type 
implementing __setitem__ the way you see fit.

Also, remember that properties are just one possible way to use the 
descriptor protocol to implement computed attributes. You can define 
your own descriptor objects.

HTH



More information about the Python-list mailing list