trap setting attribute when the attribute is dict

yosuke at ccwf.cc.utexas.edu yosuke at ccwf.cc.utexas.edu
Mon Sep 3 22:51:10 EDT 2007


John Machin <sjmachin at lexicon.net> wrote:
> On Sep 4, 5:47 am, <yos... at ccwf.cc.utexas.edu> wrote:
>> Arnaud Delobelle <arno... at googlemail.com> wrote:
>> > On Sep 3, 7:00 pm, <yos... at ccwf.cc.utexas.edu> wrote:
>>
>> >> I want to do something like this.  My class/instance has a dict as a
>> >> property.  I want the instance to catch the change in the dict (change
>> >> in some values, addition/deletion of key/value etc) to be recognized by
>> >> the class instance.
>>
>> >> How can I do this?  Any suggestions are very well appreciated.
>>

[snip]

>>
>> Thank you for suggestion, Arnaud.
>>
>> Since you are discouraging this idea of trigger, may I ask an advice of
>> if my intention was legitimate one or not?
>>
>> My intention was to have a propery 'sum' in my object, and which has sum
>> of all the values() of the dict (i have code to make sure that the value
>> of dict are all numeric).  I could just the propery being calculated
>> everytime the property got __getattr__.  But I thought that it was
>> inefficient to calculate the same number over and over when the value is
>> already known.  So I was thiking of calculating the number only when the
>> dict got modified.
>>
>> I also experimented with the idea of subclassing dict itself to
>> calculate the sum.  Is this what would be better solution?
>>

> And what's wrong with calculating the value when you need it ...

> def thedictsum(self):
>     return sum(self.thedict.itervalues())

> How big will the dict be, how often will it be updated, how often will
> the sum be required?

> Have you done timings of any of the options?

Thank you for the post.

I want the code to have fewer lines of code.  That was my biggest
motivation for this.  I have many of this dict with sum, each have <1000
elements.  all of them share the same keys.  I do many
addition/subtraction/divisions by each elements (yes, division makes the
elements not being able to summed).  so in my code i have repeated lines
that works on each elements and then on the sum.  i made a dict to take
care of adding elements by elements.  but i still had two lines of code,
one for dict elements, and one for the sum.

my code was originally in fortran and it has like thousands of lines,
obscuring the mathematics that i want to do.  i need to modify math
often while i use the code (i dont know exactly what math i will need at
this point), so i want to the code to be able to show the math
clearly.  ideally i want to this to be somthing like

c = a + b
d = 1 / c

and each of a, b, c, d knows how to operates on each elements, and also
keep track of the sum.

I think i will settle for extending dict to have sum property and keep
track of it .





> You say you have code to check that the values are numeric; why not
> put this together with maintenance of the sum into a method which
> clients must use to update the dict?

> BTW, my motto:

> "__" == "Danger, Will Robinson!" == "Wrong way, go back" :-)


not quite getting this....  :(

> HTH,
> John


-- 
yosuke kimura
Center for Energy and Environmental Resources
The Univ. of Texas at Austin, USA



More information about the Python-list mailing list