is None or == None ?

Alf P. Steinbach alfps at start.no
Fri Nov 6 17:54:53 CET 2009


* Marco Mariani:
> Alf P. Steinbach wrote:
> 
>> If you imagine that instead, for an integer variable x it stores the 
>> integer value in the variable in some other place than ordinarily used 
>> for pointer, and let the pointer point to that place in the same 
>> variable, then without checking type the 'is' operator should report 
>> false for 'x = 3; y = 3; x is y', but it doesn't with my Python 
> 
> Yes, CPython caches a handful of small, "commonly used" integers, and 
> creates objects for them upon startup. Using "x is y" with integers 
> makes no sense and has no guaranteed behaviour AFAIK
> 
>> In short, you're saying that there is an extreme inefficiency with 
>> every integer dynamically allocated /plus/, upon production of an 
>> integer by e.g. + or *, inefficiently finding the previously allocated 
>> integer of that value and pointing there,
> 
> no, it doesn't "point there":
> 
>>>>> a=1E6
>>>>> a is 1E6
>> False
>>>>> a=100
>>>>> a is 100
>> True

I stand corrected on that issue, I didn't think of cache for small values.

On my CPython 3.1.1 the cache seems to support integer values -5 to +256, 
inclusive, apparently using 16 bytes of storage per value (this last assuming 
id() just returns the address).

But wow. That's pretty hare-brained: dynamic allocation for every stored value 
outside the cache range, needless extra indirection for every operation.

Even Microsoft COM managed to get this right.

On the positive side, except that it would probably break every C module (I 
don't know), in consultant speak that's definitely a potential for improvement. :-p


Cheers,

- Alf



More information about the Python-list mailing list