Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2a.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.004 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'laura': 0.07; 'see:': 0.07; 'suppose': 0.07; 'correct,': 0.09; 'creighton': 0.09; 'explanation': 0.09; 'sure,': 0.09; 'wrong,': 0.09; 'cc:addr :python-list': 0.11; 'python': 0.11; '23,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'numpy': 0.16; 'operator.': 0.16; 'situation.': 0.16; 'skip:> 20': 0.16; 'subscripting': 0.16; 'elements': 0.16; 'wrote:': 0.18; 'feb': 0.22; '>>>': 0.22; 'import': 0.22; 'cc:addr:python.org': 0.22; "aren't": 0.24; 'documented': 0.24; 'integer': 0.24; 'lets': 0.24; "shouldn't": 0.24; 'mon,': 0.24; 'question': 0.24; 'cc:2**0': 0.24; 'compare': 0.26; 'supported': 0.26; 'header:In- Reply-To:1': 0.27; 'function': 0.29; 'correct': 0.29; '(this': 0.29; 'am,': 0.29; 'array': 0.29; 'message-id:@mail.gmail.com': 0.30; 'getting': 0.31; 'object.': 0.31; 'writes:': 0.31; 'running': 0.33; 'common': 0.35; 'created': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'everyone.': 0.36; 'object,': 0.36; 'returning': 0.36; 'should': 0.36; 'being': 0.38; 'represent': 0.38; 'list,': 0.38; 'expect': 0.39; 'according': 0.40; 'how': 0.40; 'duration': 0.60; 'break': 0.61; 'new': 0.61; 'numbers': 0.61; 'entire': 0.61; 'you.': 0.62; 'back': 0.62; 'such': 0.63; 'more': 0.64; 'between': 0.67; 'fact,': 0.69; 'safe': 0.72; 'brand': 0.72; '2015': 0.84; 'to:none': 0.92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=NF9MmTNVLo6G7Ges0F1i905ouZNuZDWXJBbRjsdxV0M=; b=MnprZh2gEk5R1HIR8B8Y72rT2ctcF+N8Ow7VP5Zv9gn+/JeW8IaDn17N65NzClJDCn NpOsRLnJeDYk2QGHTBnIa7Aiu9FOZhMWAZg2IxmW45h914s5CzfXCgyO0B3hvm+HrcqW yBbfYXfbRwo8z1f4p5sWdxrQJNMz3Pm2Y6AX1Nj2QSk4M6hIcfl3w8oLaYa4nny+8zzc toYxyWeqmILiXspKd9oUgb6R7R7kxmekg7GsHGNjT3gO2EWvtFVXzOFyj7g+2czLAEEt qUsNJ9ISM6xq6IRiWTRNsSH2ce5NFzzFEo8ou4VuTbIdLNcb/8JGziGBVE+IDjoyQRSv rhig== MIME-Version: 1.0 X-Received: by 10.50.131.196 with SMTP id oo4mr8991700igb.2.1424633033531; Sun, 22 Feb 2015 11:23:53 -0800 (PST) In-Reply-To: <201502221813.t1MIDVpD011539@fido.openend.se> References: <87f18c68-120d-44f2-bd34-6f73c69365da@googlegroups.com> <201502221813.t1MIDVpD011539@fido.openend.se> Date: Mon, 23 Feb 2015 06:23:53 +1100 Subject: Re: id() and is operator From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 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: 62 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1424633042 news.xs4all.nl 2856 [2001:888:2000:d::a6]:42953 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:86148 On Mon, Feb 23, 2015 at 5:13 AM, Laura Creighton 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