On Wed, Jul 28, 2010 at 1:28 PM, William Stein <[email protected]> wrote:
> On Wed, Jul 28, 2010 at 1:14 PM, Jason Grout
> <[email protected]> wrote:
>> Someone pointed this out to me recently, and it sounded like a useful gotcha
>> to know about python.  Apparently small python integers are cached, while
>> larger ones are not.  Sometimes.
>>
>> % sage -ipython
>> Python 2.6.4 (r264:75706, May 25 2010, 15:42:09)
>> Type "copyright", "credits" or "license" for more information.
>>
>> IPython 0.9.1 -- An enhanced Interactive Python.
>> ?         -> Introduction and overview of IPython's features.
>> %quickref -> Quick reference.
>> help      -> Python's own help system.
>> object?   -> Details about 'object'. ?object also works, ?? prints more.
>>
>> In [1]: a=1
>>
>> In [2]: b=1
>>
>> In [3]: a is b
>> Out[3]: True
>>
>> In [4]: a=1000
>>
>> In [5]: b=1000
>>
>> In [6]: a is b
>> Out[6]: False
>>
>> In [7]: a=1000; b=1000; a is b
>> Out[7]: True
>>
>>
>> Note that these are *python* integers, not Sage integers, in the example
>> above.  Note also that if the two integers are on the same line, the *are*
>> the same object.
>
>> So moral of the story: be *very* careful about where you use "is".
>
> Big +1  !  This is something that always sets of my warning bells when
> refereeing code.

Same thing goes for small/interned strings.

sage: 'aa' is 'aa'
 True

sage: 'aa' is 'a'*2
 False

- Robert

-- 
To post to this group, send an email to [email protected]
To unsubscribe from this group, send an email to 
[email protected]
For more options, visit this group at http://groups.google.com/group/sage-devel
URL: http://www.sagemath.org

Reply via email to