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


Groups > comp.lang.python > #86149

Re: id() and is operator

Date 2015-02-22 11:16 -0800
From Gary Herron <gherron@digipen.edu>
Subject Re: id() and is operator
References <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.19024.1424633079.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 02/22/2015 09:53 AM, LJ wrote:
> 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
>
>
> Any clarification is much appreciated.
>
> Cheers,


In fact, b[0] and b[2] are different objects as can be seen here:
 >>> import numpy as np
 >>> b=np.array([[0]*2]*3)
 >>> b[0]=1 // broadcast into both ints in row 0
 >>> b[1]=2 // ... row 1
 >>> b[2]=3 // ... row 2
 >>> b
array([[1, 1],
        [2, 2],
        [3, 3]])

When you extracted b[0], you got a newly created python/numpy object 
(1x2 array of ints) briefly stored at location  45855552 but then 
deleted immediately after that use.  A little later, the extraction of 
b[2] used the same bit of memory.  The id of a temporarily created value 
is meaningless, and apparently misleading.

As a separate issue, each of b, b[0], b[1], and b[2] do *all* refer to 
the same underlying array of ints as can be seen here:
 >>> r = b[0]
 >>> r[0] = 123
 >>> b
array([[123,   1],
        [  2,   2],
        [  3,   3]])


but the Python/numpy objects that wrap portions of that underlying array 
of ints are all distinct.


Gary Herron



-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

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