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


Groups > comp.lang.python > #86140

Re: id() and is operator

From Laura Creighton <lac@openend.se>
Subject Re: id() and is operator
References <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com>
Date 2015-02-22 19:13 +0100
Newsgroups comp.lang.python
Message-ID <mailman.19019.1424628820.18130.python-list@python.org> (permalink)

Show all headers | View raw


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.

So.

  Python 2.7.9 (default, Dec 11 2014, 08:58:12)
 [GCC 4.9.2] on linux2
 Type "help", "copyright", "credits" or "license" for more information.
 >>> a = 1
 >>> b = 1
 >>> a is b
 True
 >>> a = 1001
 >>> b = 1001
 >>> a is b
 False

--------
Don't rely on this.  Other implementations are free to implement this
however they like.
--------

[PyPy 2.4.0 with GCC 4.9.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>> a = 1001
>>>> b = 1001
>>>> a is b
True

Laura

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