The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

import-bot (20211) [Avatar] Offline
#1
[Originally posted by haasm]

After having read p. 98 (sameness of objects) I thought I understand the
point. But:

First case:

>>> x = 4-3
>>> y = 1
>>> x is y
1

I understand that x == y. But why is 1 the same object?

Second case:

>>> a = [1,2,3,4]
>>> b = a[0]
>>> b is a[0]
1
>>> c = [1,2,3,4]
>>> b = c[0]
>>> b is a[0]
1
>>> b is c[0]
1

How can be object identity when b is at the same time a[0] _and_ c[0]?

Max
import-bot (20211) [Avatar] Offline
#2
Re: same objects
[Originally posted by daryl harms]

> After having read p. 98 (sameness of objects) I thought I understand the
> point. But:
>
> First case:
>
> >>> x = 4-3
> >>> y = 1
> >>> x is y
> 1
>
> I understand that x == y. But why is 1 the same object?
>
Here the integer 1 itself is the object that both x and y refer to
That is (using ascii to try and draw the picture):

x----

y------1


> Second case:
>
> >>> a = [1,2,3,4]
> >>> b = a[0]
> >>> b is a[0]
> 1
> >>> c = [1,2,3,4]
> >>> b = c[0]
> >>> b is a[0]
> 1
> >>> b is c[0]
> 1
>
> How can be object identity when b is at the same time a[0] _and_ c[0]?

What happens in this example is that b, a[0] and c[0] all end up referring
to the same object, i.e. the integer 1.
This looks like (again trying to draw a picture in ascii):

c = [ , , , ]

b------1 2 3 4
/ / / /
a = [/,/,/,/]


If the example were changed to be:
>>> a = [[1],2,3,4]
>>> b = a[0]
>>> b is a[0]
1
>>> c = [[1],2,3,4]
>>> b = c[0]
>>> b is a[0]
0
>>> b is c[0]
1

This is I believe the result you were expecting. Now, c[0] and b refer to the
same object (i.e. a list whose only element refers to the integer 1). But a[0]
refers to a different object (i.e. a different list whose only element also
refers to 1).

c = [ , , , ]

b-----[]

1 2 3 4
/ / / /
[] / / /
/ / / /
a = [/,/,/,/]
>
> Max

I hope this helps (although it would be clearer to show with proper
diagrams!).

This is a good question and often a source of confusion for people. I see that
in the diagrams on page 61 and 62 this would have been clearer if I hadn't
taken the shortcut of not showing this last level of indirection to the
integer objects.

Daryl