[Python-Dev] Caching float(0.0)

Nick Craig-Wood nick at craig-wood.com
Sun Oct 1 11:43:38 CEST 2006


On Sat, Sep 30, 2006 at 03:21:50PM -0700, Bob Ippolito wrote:
> On 9/30/06, Terry Reedy <tjreedy at udel.edu> wrote:
> > "Nick Coghlan" <ncoghlan at gmail.com> wrote in message news:451E31ED.7030905 at gmail.com...
> > > I suspect the problem would typically stem from floating point
> > > values that are read in from a human-readable file rather than
> > > being the result of a 'calculation' as such:

Over a TCP socket in ASCII format for my application

> > For such situations, one could create a translation dict for both common
> > float values and for non-numeric missing value indicators.  For instance,
> > flotran = {'*': None, '1.0':1.0, '2.0':2.0, '4.0':4.0}
> > The details, of course, depend on the specific case.
> 
> But of course you have to know that common float values are never
> cached and that it may cause you problems. Some users may expect them
> to be because common strings and integers are cached.

I have to say I was surprised to find out how many copies of 0.0 there
were in my code and I guess I was subconsciously expecting the
immutable 0.0s to be cached even though I know consciously I've never
seen anything but int and str mentioned in the docs.

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick


More information about the Python-Dev mailing list