[Numpy-discussion] np.nan and ``is``

Andrew Dalke dalke at dalkescientific.com
Fri Sep 19 22:51:44 EDT 2008


On Sep 19, 2008, at 10:04 PM, Christian Heimes wrote:
> Andrew Dalke wrote:
>> There are a few things that Python-the-language guarantees are  
>> singleton
>> objects which can be compared correctly with "is".

> The empty tuple () and all interned strings are also guaranteed to be
> singletons.

Where's the guarantee?  As far as I know it's not part of
Python-the-language, and I thought it was only an implementation
detail of CPython.

tupleobject.c says:

PyTuple_Fini(void)
{
#if PyTuple_MAXSAVESIZE > 0
         /* empty tuples are used all over the place and applications  
may
          * rely on the fact that an empty tuple is a singleton. */
         Py_XDECREF(free_list[0]);
         free_list[0] = NULL;

         (void)PyTuple_ClearFreeList();
#endif
}

but that doesn't hold under Jython 2.2a1:


Jython 2.2a1 on java1.4.2_16 (JIT: null)
Type "copyright", "credits" or "license" for more information.
 >>> () is ()
0
 >>> 1 is 1
1



> String interning is used to optimize code on C level. It's
> much faster to compare memory addresses than objects. All strings  
> can be
> interned through the builtin function intern like s = intern(s). For
> Python 3.x the function was moved in the the sys module and changed to
> support str which are PyUnicode objects.

"intern" being listed in the documentation under
     http://docs.python.org/lib/non-essential-built-in-funcs.html

     2.2 Non-essential Built-in Functions

     There are several built-in functions that are no longer

     essential to learn, know or use in modern Python programming.

     They have been kept here to maintain backwards compatibility

     with programs written for older versions of Python.




Again, I think this is only an aspect of the CPython implementation.



> The Python core makes no difference between quiet NaNs and signaling
> NaNs.

Based on my limited readings just now, it seems that that's the general
consensus:

   http://www.open-std.org/jtc1/sc22/wg14/www/docs/n965.htm
   """Standard C only adopted Quiet NaNs. It did not adopt Signaling
   NaNs because it was believed that they are of too limited
   utility for the amount of work required."""

   http://www.digitalmars.com/d/archives/digitalmars/D/ 
signaling_NaNs_and_quiet_NaNs_75844.html
   "Signaling NaNs have fallen out of favor. No exceptions get raised  
for them."

   http://en.wikipedia.org/wiki/NaN
   """There were questions about if signalling NaNs should continue  
to be
   required in the revised standard. In the end it appears they will
   be left in."""



> We were discussion the possibility of a NaN singleton during
> our revamp of Python's IEEE 754 and math support for Python 2.6 and  
> 3.0.
> But we decided against it because the extra code and cost wasn't worth
> the risks. Instead I added isnan() and isinf() to the math module.

I couldn't find that thread.  What are the advantages of converting
all NaNs to a singleton?  All I can come up with are disadvantages.

BTW, another place to look is the Decimal module

 >>> import decimal
 >>> decimal.Decimal("nan")
Decimal("NaN")
 >>>

Looking at the decimal docs now I see a "canonical()" method which

    The result has the same value as the operand but always
    uses a canonical encoding. The definition of canonical
    is implementation-defined; if more than one internal
    encoding for a given NaN, Infinity, or finite number
    is possible then one ‘preferred’ encoding is deemed
    canonical. This operation then returns the value using
    that preferred encoding.



				Andrew
				dalke at dalkescientific.com





More information about the NumPy-Discussion mailing list