Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86148

Re: id() and is operator

References <luisjosenovoa@gmail.com> <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com> <201502221813.t1MIDVpD011539@fido.openend.se>
Date 2015-02-23 06:23 +1100
Subject Re: id() and is operator
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.19023.1424633042.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Feb 23, 2015 at 5:13 AM, Laura Creighton <lac@openend.se> wrote:
> In a message of Sun, 22 Feb 2015 09:53:33 -0800, LJ writes:
>>Hi everyone. Quick question here. Lets suppose if have the following numpy array:
>>
>>b=np.array([[0]*2]*3)
>>
>>and then:
>>
>>>>> id(b[0])
>>45855552
>>>>> id(b[1])
>>45857512
>>>>> id(b[2])
>>45855552
>>
>>Please correct me if I am wrong, but according to this b[2] and b[0] are the same object. Now,
>>
>>>>> b[0] is b[2]
>>False
>
>
> You are running into one of the peculiarities of the python representation
> of numbers.  It can make things more efficient to represent all common
> numbers as 'there is only one' of them.

That shouldn't break the correspondence between id() and the is
operator. The id function is documented as returning an integer which
is "guaranteed to be unique among simultaneously existing objects",
and if all three elements of b exist through the entire duration of
this experiment, it should be perfectly safe to compare their id()s to
check object identity.

So the only explanation I can think of is: When you subscript a numpy
array, you aren't getting back a reference to a pre-existing object,
but you are instead getting a brand new object which is being created
for you. (This theory is supported by a vague recollection that
subscripting a numpy array returns a view of some sort, but you'd have
to check the docs.) If that theory is correct, then you'd expect to
find that the id() of such a thing is not stable; and that is, in
fact, what I see:

>>> import numpy as np
>>> b=np.array([[0]*2]*3)
>>> id(b[0])
26806960
>>> id(b[0])
26655344
>>> id(b[0])
26820432
>>> id(b[0])
26806960
>>> id(b[0])
26655344

After a few iterations, they're getting reused, but it's not like
playing with a Python list, where you would be getting back the exact
same object every time

You'd have to check the docs to be sure, but this is how I would go
about exploring the situation.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

id() and is operator LJ <luisjosenovoa@gmail.com> - 2015-02-22 09:53 -0800
  Re: id() and is operator Laura Creighton <lac@openend.se> - 2015-02-22 19:13 +0100
  Re: id() and is operator Chris Angelico <rosuav@gmail.com> - 2015-02-23 06:23 +1100
  Re: id() and is operator Gary Herron <gherron@digipen.edu> - 2015-02-22 11:16 -0800
  Re: id() and is operator Laura Creighton <lac@openend.se> - 2015-02-22 20:58 +0100
  Re: id() and is operator Marko Rauhamaa <marko@pacujo.net> - 2015-02-22 23:25 +0200
    Re: id() and is operator Chris Angelico <rosuav@gmail.com> - 2015-02-23 08:36 +1100
    Re: id() and is operator Terry Reedy <tjreedy@udel.edu> - 2015-02-23 01:02 -0500
      Re: id() and is operator Marko Rauhamaa <marko@pacujo.net> - 2015-02-23 08:31 +0200
    Re: id() and is operator Gary Herron <gherron@digipen.edu> - 2015-02-22 22:29 -0800
  Re: id() and is operator Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-02-23 12:31 +1100
  Re: id() and is operator Terry Reedy <tjreedy@udel.edu> - 2015-02-23 01:14 -0500

csiph-web