On Wed, Jul 28, 2010 at 1:14 PM, Jason Grout <jason-s...@creativetrax.com> 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. E.g., I don't like this sort of construction, but saw it recently in a patch on trac: def f(n=1): if n is 1: ... else: ... -- William > > Thanks, > > Jason > > -- > To post to this group, send an email to sage-devel@googlegroups.com > To unsubscribe from this group, send an email to > sage-devel+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/sage-devel > URL: http://www.sagemath.org > -- William Stein Professor of Mathematics University of Washington http://wstein.org -- To post to this group, send an email to sage-devel@googlegroups.com To unsubscribe from this group, send an email to sage-devel+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sage-devel URL: http://www.sagemath.org