Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86149
| Path | csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <gherron@digipen.edu> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.014 |
| X-Spam-Evidence | '*H*': 0.97; '*S*': 0.00; 'memory.': 0.07; 'suppose': 0.07; 'extracted': 0.09; 'newly': 0.09; 'portions': 0.09; 'wrong,': 0.09; 'stored': 0.12; '2],': 0.16; '[2,': 0.16; 'numpy': 0.16; 'underlying': 0.16; 'wrote:': 0.18; 'bit': 0.19; '>>>': 0.22; 'import': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'issue,': 0.24; 'lets': 0.24; 'cheers,': 0.24; 'question': 0.24; 'header:In-Reply-To:1': 0.27; 'correct': 0.29; 'appreciated.': 0.29; 'am,': 0.29; 'array': 0.29; '>>>>': 0.31; 'apparently': 0.31; 'gary': 0.31; 'object.': 0.31; 'created': 0.35; 'objects': 0.35; 'but': 0.35; 'everyone.': 0.36; 'false': 0.36; 'to:addr:python-list': 0.38; 'little': 0.38; 'use.': 0.39; 'to:addr:python.org': 0.39; 'according': 0.40; 'temporarily': 0.60; 'here:': 0.62; 'refer': 0.63; 'different': 0.65; 'charset:windows-1252': 0.65; 'broadcast': 0.68; 'fact,': 0.69; 'institute': 0.72; 'received:204': 0.75; 'dr.': 0.77 |
| Date | Sun, 22 Feb 2015 11:16:56 -0800 |
| From | Gary Herron <gherron@digipen.edu> |
| User-Agent | Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 |
| MIME-Version | 1.0 |
| To | python-list@python.org |
| Subject | Re: id() and is operator |
| References | <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com> |
| In-Reply-To | <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com> |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.19024.1424633079.18130.python-list@python.org> (permalink) |
| Lines | 66 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1424633079 news.xs4all.nl 2878 [2001:888:2000:d::a6]:44334 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:86149 |
Show key headers only | 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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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