Path: csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!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; 'python.': 0.02; 'correct.': 0.07; 'memory.': 0.07; 'variables': 0.07; '[1,': 0.09; 'creation,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; '\xe2\x80\x94': 0.09; 'python': 0.11; 'behave': 0.16; 'distinct': 0.16; 'finney': 0.16; 'immutable,': 0.16; 'made,': 0.16; 'objects.': 0.16; 'rather,': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'requested.': 0.16; 'subject:Object': 0.16; 'written': 0.21; 'seems': 0.21; '>>>': 0.22; 'memory': 0.22; 'separate': 0.22; 'header:User-Agent:1': 0.23; 'integer': 0.24; 'decide': 0.24; 'helpful': 0.24; 'references': 0.26; 'read,': 0.26; 'somewhere': 0.26; 'header:X -Complaints-To:1': 0.27; 'point': 0.28; "doesn't": 0.30; 'list:': 0.30; 'object.': 0.31; 'values.': 0.31; 'writes:': 0.31; 'lists': 0.32; 'languages': 0.32; 'subject: (': 0.35; 'beyond': 0.35; 'objects': 0.35; 'one,': 0.35; 'but': 0.35; 'there': 0.35; 'really': 0.36; 'false': 0.36; 'object,': 0.36; 'received:com.au': 0.36; 'two': 0.37; 'list': 0.37; 'ben': 0.38; 'machines': 0.38; 'to:addr:python-list': 0.38; 'list,': 0.38; 'that,': 0.38; 'short': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'how': 0.40; 'new': 0.61; 'matter': 0.61; 'times': 0.62; 'guarantee': 0.63; 'choose': 0.64; 'different': 0.65; 'skip:\xe2 10': 0.65; 'here': 0.66; 'dont': 0.67; 'detail.': 0.68; 'guaranteed': 0.75; 'behavior': 0.77; '8bit%:46': 0.78; 'answer:': 0.84; 'different.': 0.84; 'received:125': 0.84; 'differences': 0.93; 'this!': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Ben Finney Subject: Object identity (was: Reference) Date: Mon, 03 Mar 2014 21:00:18 +1100 References: <53144e8d$0$2149$426a74cc@news.free.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Gmane-NNTP-Posting-Host: vmx15867.hosting24.com.au X-Public-Key-ID: 0xBD41714B X-Public-Key-Fingerprint: 9CFE 12B0 791A 4267 887F 520C B7AC 2E51 BD41 714B X-Public-Key-URL: http://www.benfinney.id.au/contact/bfinney-gpg.asc X-Post-From: Ben Finney User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Cancel-Lock: sha1:DF1Dg29ve0B83MYWSSr4K+a3B+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: 1393840833 news.xs4all.nl 2968 [2001:888:2000:d::a6]:52542 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:67522 "ast" writes: > >>> A=7 > >>> B=7 > >>> A is B > True > > I understand that there is a single object 7 somewhere in memory and > both variables A and B point toward this object 7 Try not to think in terms of “point to”. Rather, the names “A” and “B” are bound to that object. The distinction is subtle; but it's important to realise that *all* references in Python do this, and there's no way to talk about an object in Python without using a reference. The “pointer” model from other languages doesn't exist in Python. > now do the same with a list: > > >>> l1 = [1, 2] > >>> l2 = [1, 2] > >>> l1 is l2 > False > > It seems this time that there are 2 distincts objects [1, 2] in > memory. That's correct. > I dont really understand why the behavior is different. Both integer 7 > and list [1, 2] are objects. Why is it different ? Short answer: object identity is an implementation detail. It's up to the Python implementation to decide when to re-use an object when a new one is requested. No guarantee is made, when you ask to create an object, that you won't get an existing one if that would work just as well. Since the integer object 7 is immutable, it will behave the same no matter how many times you ask for a new one, the Python implementation can choose to give you the same object. But it might not — don't depend on this! Since two separate lists are mutable, each one can have a distinct history after creation, so it would be less helpful to return an existing list when you ask for a new one. But again, there's no guarantee here either! A Python implementation might decide to give you an existing list, if existing guarantees can be kept. The moral is: Don't depend on differences in object identity. You can be guaranteed that an object will retain its own identity, and its identity will always be different from all other co-existing objects that have different values. Beyond that, don't make any assumptions. -- \ “Programs must be written for people to read, and only | `\ incidentally for machines to execute.” —Abelson & Sussman, | _o__) _Structure and Interpretation of Computer Programs_ | Ben Finney