[Tutor] Object comparison vs. Identity

Wesley J. Chun wesc@deirdre.org
Sun, 11 Mar 2001 17:57:53 -0800


    > From: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
    > Date: Sat, 10 Mar 2001 22:36:52 -0800 (PST)
    > 
    > On Sat, 10 Mar 2001, Sheila King wrote:
    > 
    > > On pp. 84 - 85 he is discussing object identity, and
    > > presents the following example:
    > > foo1 = 4
    > > foo2 = 3 + 1
    > > He says that the first statement creates a numeric object and assigns it to
    > > foo1. The second statement creates a numeric object, and assigns it to foo2.
    > > Although the value of the two objects are the same, he claims that they are
    > > two distinct objects.
    > 
    > True; however, the reason that this doesn't work for small numbers is
    > because the integers are precached --- Python keeps instances of the
    > integers from -1 to 100 to reduce the cost of using these popular numbers.
    > We can empirically see this with a small experiment:
    >	: 


first, i'd like to thank Danny publically for covering for me
in this particular example.  Python does cache what it perceives
as "high-use" integers (as well as some strings).

my example can also be reworked by using floating point values:

>>> foo1 = 4.0
>>> foo2 = 3.0 + 1.0
>>> foo1
4.0
>>> foo2
4.0
>>> id(foo1)
906864
>>> id(foo2)
906816

i will make sure this boo-boo is annoted in the errata as well
as evicted in the next edition.

thanks, and sorry sheila for the confusion!

-wesley

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
Silicon Valley-SF Bay Area Python users group:  http://www.baypiggies.org

"Core Python Programming", Prentice Hall PTR, TBP Fall 2000
    http://www.phptr.com/ptrbooks/ptr_0130260363.html

wesley.j.chun :: wesc@baypiggies.org or cyberweb_consulting@yahoo.com
cyberweb.consulting :: silicon.valley, ca
http://www.roadkill.com/~wesc/cyberweb/