[Tutor] Object comparison vs. Identity
Pedro Diaz Jimenez
pdiaz88@terra.es
Sat, 10 Mar 2001 20:43:12 +0100
Maybe the author wants to explain that foo1 and foo2 are two diferent
instances of the numeric (or whatever) class but they hold the same numeric
value
Cheers
Pedro
On Sunday 11 March 2001 03:22, Sheila King wrote:
> I recently received one of the Python books I ordered:
> Core Python Programming by Wesley Chun
>
> Seems OK. Since I've already programmed in a few different languages, and
> worked through the Python tutorial, I'm doing fine with it. It is
> definitely not a book for someone who has never programmed before.
>
> Anyhow, to my question...
>
> 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.
> (I read this as implying, that while
> foo1==foo2 would return 1,
> he is saying that id(foo1) == id(foo2),
> or alternatively foo1 is foo2
> would both return 0.)
>
> Now, on my Win98 machine, in an interactive IDLE session, I got the
following:
> >>> x = 4
> >>> y = 3+1
> >>> id(x)
>
> 8400780
>
> >>> id(y)
>
> 8400780
>
> >>> y is x
>
> 1
>
>
> I also typed up a similar example, saved it in a file, and ran it from a
> DOS prompt, and got the same results. 3+1 did not create a different object
> for me than 4.
>
> Is this something I should write to the author about, and let him know
> there is an error? Or is there something I'm not picking up on going on
> here?
>
> --
> Sheila King
> http://www.thinkspot.net/sheila/
> http://www.k12groups.org/
>
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor