numpy NaN, not surviving pickle/unpickle?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sun Sep 13 19:53:26 EDT 2009


On Sun, 13 Sep 2009 17:58:14 -0500, Robert Kern wrote:

> John Ladasky wrote:
> 
>> In my leisure time, I would like to dig deeper into the issue of why
>> object identities are not guaranteed for elements in numpy arrays...
>> with elements of type "float", at least, I thought this would be
>> trivial.
> 
> Why do you think that? We would have to keep a reference around to every
> scalar object that gets created and check against that cache whenever
> someone accesses an element in order to reuse the previously created
> object. That slows element access down for essentially no benefit.


Exactly -- there are 2**53 distinct floats on most IEEE systems, the vast 
majority of which might as well be "random". What's the point of caching 
numbers like 2.5209481723210079? Chances are it will never come up again 
in a calculation.

There may be something to be said for caching "common" floats, like pi, 
small integers (0.0, 1.0, 2.0, ...), 0.5, 0.25 and similar, but I doubt 
the memory savings would be worth the extra complexity.

You can do your own caching: pass every calculation result through the 
following:

_cache = {}
def cache(f):
    """Cache and return float f."""
    if f in _cache:
        return _cache[f]
    _cache[f] = f
    return f




-- 
Steven



More information about the Python-list mailing list