How can I use __setitem__ method of dict object?

Bruno Desthuilliers bdesth.quelquechose at free.quelquepart.fr
Wed Feb 7 16:08:42 EST 2007


jeremito a écrit :
(snip)
>>
>>Thanks again!  Sometimes the problem is simply not knowing where to
>>find the documentation, 

http://python.org/doc is usually a good start !-)

>>or finding the right portion of the
>>documentation. 

Yes, this is not always obvious. FWIW, browsing docs is probably the 
most time-consuming task of programmers.

> One more question.  I will be asking for the value of cs.xT *many*
> (~millions) times.  Therefore I don't want it's value to be calculated
> on the fly.

As Jussi said, first make it right. And then, *if* you have a *real* 
performance problem, profile your code to find where bottlenecks really are.

>  How can I set the value of xT whenever xS, xF, or xG are
> changed, but not allow it to be set directly?  From the example given
> previously, it seems like it can't be done this way.

A naive solution might be to turn all these attributes into properties, 
so you could recompute and store the values of xA and xT each time xS, 
xF or xG are modified. The example I gave you should be enough to get 
you started here. But beware: in Python, function calls are somewhat 
expansive, so you may end up slowing down your code.

Once again, first focus on cleanly solving the problem, and if *and only 
if* you have objective performance problems, then *profile* your code 
before trying to solve the wrong problem.



More information about the Python-list mailing list