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: 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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 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