Explanation of list reference

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sat Feb 15 11:53:48 CET 2014


On Sat, 15 Feb 2014 10:44:39 +0100, Christian Gollwitzer wrote:

> Am 15.02.14 01:57, schrieb Chris Angelico:
>> Can you give an example of an ambiguous case? Fundamentally, the 'is'
>> operator tells you whether its two operands are exactly the same
>> object, nothing more and nothing less, so I assume your "ambiguous
>> cases" are ones where it's possible for two things to be either the
>> same object or two indistinguishable ones.
> 
> What about the thing I posted down in this thread?
> 
>  >>> import numpy as np
>  >>> a=np.array([1, 2, 3, 4])
>  >>> b=a[:]
>  >>> id(a)
> 140267900969344
>  >>> id(b)
> 140267901045920
> 
> So, a and b are different things, right?

Correct. They are different objects. But they may share underlying state.


>  >>> b[1]=37
>  >>> b
> array([ 1, 37,  3,  4])
>  >>> a
> array([ 1, 37,  3,  4])

And indeed numpy arrays do share state. Why? No idea. Somebody thought 
that it was a good idea. (Not me though...)


> Still they are connected. I can imagin that id() is just a debugging
> tool for extensions. What useful applications does it have outside of
> this?

Very few.



-- 
Steven



More information about the Python-list mailing list