On Sun, 1 Oct 2006 13:54:31 -0400, Terry Reedy <[EMAIL PROTECTED]> wrote: > >"Nick Craig-Wood" <[EMAIL PROTECTED]> wrote in message >news:[EMAIL PROTECTED] >> On Fri, Sep 29, 2006 at 12:03:03PM -0700, Guido van Rossum wrote: >>> I see some confusion in this thread. >>> >>> If a *LITERAL* 0.0 (or any other float literal) is used, you only get >>> one object, no matter how many times it is used. >> >> For some reason that doesn't happen in the interpreter which has been >> confusing the issue slightly... >> >> $ python2.5 >>>>> a=0.0 >>>>> b=0.0 >>>>> id(a), id(b) >> (134737756, 134737772) > >Guido said *a* literal (emphasis shifted), reused as in a loop or function >recalled, while you used *a* literal, then *another* literal, without >reuse. Try a=b=0.0 instead.
Actually this just has to do with, um, "compilation units", for lack of a better term: [EMAIL PROTECTED]:~$ python Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = 0.0 >>> b = 0.0 >>> print a is b False >>> ^D [EMAIL PROTECTED]:~$ cat > test.py a = 0.0 b = 0.0 print a is b ^D [EMAIL PROTECTED]:~$ python test.py True [EMAIL PROTECTED]:~$ cat > test_a.py a = 0.0 ^D [EMAIL PROTECTED]:~$ cat > test_b.py b = 0.0 ^D [EMAIL PROTECTED]:~$ cat > test.py from test_a import a from test_b import b print a is b ^D [EMAIL PROTECTED]:~$ python test.py False [EMAIL PROTECTED]:~$ python Python 2.4.3 (#2, Apr 27 2006, 14:43:58) [GCC 4.0.3 (Ubuntu 4.0.3-1ubuntu5)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> a = 0.0; b = 0.0 >>> print a is b True >>> [EMAIL PROTECTED]:~$ Each line in an interactive session is compiled separately, like modules are compiled separately. With the current implementation, literals in a single compilation unit have a chance to be "cached" like this. Literals in different compilation units, even for the same value, don't. Jean-Paul _______________________________________________ Python-Dev mailing list Python-Dev@python.org http://mail.python.org/mailman/listinfo/python-dev Unsubscribe: http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com