Explanation of list reference

Chris Angelico rosuav at gmail.com
Fri Feb 14 22:33:36 EST 2014


On Sat, Feb 15, 2014 at 2:14 PM, Rustom Mody <rustompmody at gmail.com> wrote:
> On Saturday, February 15, 2014 8:12:14 AM UTC+5:30, Chris Angelico wrote:
>> Well, for a start, I'd use Python 3, so there's no need to explain why
>> some numbers have an L after them :)
>
> Nice point!
> And only sharpens what I am saying -- python 3 is probably more confusing than
> 2 wrt object identity

How so? Py3 eliminates an unnecessary difference:

>>> 1L == 1
True
>>> 1L is 1
False

In Py3, this can't happen, because there simply is no distinction.

(That said, the Py3 unification does mean that small integers pay the
performance cost of long integers. I've mentioned before that it may
be worth having an "under the covers" optimization whereby small
integers are stored in a machine word - but this should be utterly
invisible to the user. As far as the programmer's concerned, an int is
an int is an int.)

>> When it's utterly impossible for it to matter in any way, Python is
>> allowed to reuse objects.
>>
>> I think that's simple enough to explain. There's nothing you can do to
>> distinguish one 6 from another, so Python's allowed to have them the
>> same.
>
> Simple??
>
>>>> x=1234
>>>> y=1234
>>>> x is y
> False
>>>> 1234 is 1234
> True
>>>> x=123
>>>> y=123
>>>> x is y
> True

In all three cases, Python is allowed to use separate objects. Nothing
forces them to be shared. But in all three cases, there's no way you
could distinguish one from another, so Python's allowed to reuse the
same object.

> "utterly impossible to matter"...
> "nothing you can do to distinguish one 6 from another"
>
> All depend on doing one of these 3 for dealing with object identity
> 1. Circular definition
> 2. Delve into implementation
> 3. Wildly wave the hands

How do you distinguish between any other identical things? Once you've
decided that they're equal, somehow you need to separate identity from
value. I could have three six-sided dice, all made from the same
mould, and yet each one is a separate object. If I hold all three in
my hand and toss them onto the table, can I recognize which one is
which? No, they're identical. Are they distinct objects? Yes.

ChrisA



More information about the Python-list mailing list