Augmented assignment again

Niels Diepeveen niels at endea.demon.nl
Wed Aug 30 14:54:52 EDT 2000


Roeland Rengelink schreef:

> My users will be astronomers that want to do
> 
> reduced_data = (raw_data-bias)/flatfield
> 
> where raw_data may be a stack of, say 30 2k x 4k images. They will not
> like an extraneous copy of 1 GB worth of data, and they will not read
> the manual.
> So, either I don't supply binary arithemtic operations (which they will
> not
> like/understand), or I solve this.

If it's really important (i.e. if you're willing to do some hard work
for it), you might think about lazy evaluation. You could have __add__
return an object that doesn't contain the actual value, but just a
recipe for getting it. Then when the user calls some method for which
you need the value, you can figure out the cheapest way to compute it.
This has the possible advantage that something like
 x = a - b
 y = x / c
can also be optimized if x isn't used directly, just as an intermediate
result.
It's harder to do this correctly if the objects need to be mutable, but
that can be solved using a copy-on-write mechanism.

-- 
Niels Diepeveen
Endea automatisering




More information about the Python-list mailing list