[Tutor] Object references and caching/interning
Kent Johnson
kent37 at tds.net
Tue Dec 15 21:53:35 CET 2009
On Tue, Dec 15, 2009 at 11:43 AM, Bjorn Egil Ludvigsen
<bludvigsen at gmail.com> wrote:
> Hi,
>
> This is my frist post here. I am learning Python and Qt and am trying to
> understand a pecularity with object references. I think I get the integer
> part, but for floating point it seems like the caching/interning process
> works a little bit here and there:
>
> 1) Same object on command line
>>>> a, b = 5.0, 5.0
>>>> id(a), id(b)
> (11140648, 11140648)
> 2) Different object on command line
>>>> a = 5.0
>>>> b = 5.0
>>>> id(a), id(b)
> (11140616, 11140600)
>
> 3) Same object in script
> When running the last assignments for the float objects in a script (point
> 2) above, one line per assignment), it is cached again and the variables
> refer to the same object. Why is it different on a command line and in a
> script?
>
> How can you know when you create a new object or not, are there any rules?
I don't know why it is different, but really, you shouldn't worry
about it. Any caching or interning is implementation dependent and it
is rare that you need to worry about ids or object identity (vs
equality) except in special cases like None or marker objects that you
create.
Kent
More information about the Tutor
mailing list