Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!217.188.199.168.MISMATCH!takemy.news.telefonica.de!telefonica.de!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.049 X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'python,': 0.02; 'cpython': 0.05; 'messages.': 0.05; 'exist,': 0.09; 'mem': 0.09; 'parsing': 0.09; 'cc:addr:python-list': 0.11; 'language,': 0.12; '(note': 0.16; 'addr': 0.16; 'allocates': 0.16; 'dict': 0.16; 'storing': 0.16; 'language': 0.16; 'dependent': 0.19; 'finished': 0.19; 'later': 0.20; 'thanks.': 0.20; 'cc:addr:python.org': 0.22; 'headers': 0.24; 'instance,': 0.24; 'integer': 0.24; 'initial': 0.24; 'cc:2**0': 0.24; "i've": 0.25; 'possibly': 0.26; 'this:': 0.26; 'header:In-Reply-To:1': 0.27; 'chris': 0.29; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; '(which': 0.31; 'code': 0.31; 'lines': 0.31; 'apparently': 0.31; 'continues': 0.31; 'object.': 0.31; 'produces': 0.31; 'seemingly': 0.31; 'though.': 0.31; 'another': 0.32; 'beginning': 0.33; 'used,': 0.33; 'received:74.125.82': 0.34; 'could': 0.34; 'problem': 0.35; 'info': 0.35; 'objects': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'entry': 0.36; 'example,': 0.37; 'virtual': 0.37; 'clear': 0.37; 'easily': 0.37; 'being': 0.38; 'sometimes': 0.38; 'server': 0.38; 'same.': 0.38; 'stable': 0.38; 'received:74.125': 0.39; 'bad': 0.39; 'called': 0.40; 'even': 0.60; 'read': 0.60; 'logs': 0.60; 'subject:Can': 0.60; 'numbers': 0.61; 'simple': 0.61; "you're": 0.61; 'back': 0.62; 'connecting': 0.64; 'great': 0.65; 'charset:iso-2022-jp': 0.65; 'to:addr:gmail.com': 0.65; 'mailbox': 0.68; 'compliant': 0.68; 'detail.': 0.68; 'caused': 0.69; 'guaranteed': 0.75; 'believe,': 0.84; 'informed.': 0.84; 'skip:\x1b 30': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'destination': 0.91; 'imagine': 0.93 X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=0SOcGc/UhoT9shZfqQSqF0z3lRT912uRIgiUHjZLc9s=; b=HlYE4UxAKrPFnpc05JRcwxoPGWb1Gs/7B1MGNPTNi0alj1VhI5ARuOikyWsMOZzzrp P1OixwKJ29w/ufJHQWykGTDr0KzfV9a2o+KkBvX57+vJmP/7+H0dNLhQyaab7hJF8roN kLNmwKdqoYcQNn8KVOYvlWHCauImySObOsosI31x58SgZlsZGxeB76WD5AT5L5M7G0eB 6bKCM9wMJUpBuJZsgyhGSspZO0voapA+iDMvSwNpLnKICSRO9t+tNtpUwOpe6vNFeO7E SshWGlKVY++A5iZNPOjskA4MkoB64M+1JDk9BZMopO073wzoF3/ukmA54nDw23tsE2+A 96uA== X-Gm-Message-State: ALoCoQmwlbG6ehEZQnI0votWmjxvlXsBmaN9YOEpEuHyCBJy2eBIiAmR2yKlvGZoEJu/J2J7AZ9e MIME-Version: 1.0 X-Received: by 10.180.221.72 with SMTP id qc8mr66436234wic.19.1417233356017; Fri, 28 Nov 2014 19:55:56 -0800 (PST) X-Originating-IP: [143.89.96.134] In-Reply-To: References: <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> Date: Sat, 29 Nov 2014 11:55:55 +0800 Subject: Re: Can you use self in __str__ From: Shiyao Ma To: Chris Angelico Content-Type: text/plain; charset=ISO-2022-JP Content-Transfer-Encoding: 7bit Cc: Python 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: 50 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1417233725 news.xs4all.nl 2858 [2001:888:2000:d::a6]:42600 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:81711 2014-11-29 11:36 GMT+08:00 Chris Angelico : > You can use id() on any object. You are guaranteed to get back an > integer which is both stable and unique among all ids of objects that > exist at the same time as the one you called it on. For as long as the > object continues to exist, that number *will* stay the same. Sometimes > that's all you need; for instance, imagine a simple mail server which > produces logs like this: > > [142857] Beginning processing of message > [142857] Parsing headers > [314159] Beginning processing of message > [314159] Parsing headers > [142857] Storing in destination mailbox > [314159] Connecting to destination server > [142857] Finished processing of message > [314159] Message accepted by destination > [271871] Beginning processing of message > [314159] Finished processing of message > > You can easily see, from the initial numbers, what log lines are > associated with what messages. (Note that emails have their own IDs, > which could in theory be used, but the id() of an internal dict can be > used even before the email's ID has been read - as you see from the > example, a log entry for "Parsing headers" has to be made prior to > info from the headers being used.) It's not a problem if another > 142857 comes up later on; there's a very clear begin and end to the > message, and you're guaranteed that nothing can possibly be > interspersed with a colliding ID. > > In other implementations of Python, these numbers might look less > arbitrary (Jython, I believe, allocates them sequentially); but the > code will work just as well on any compliant implementation of the > language, because everything I've said above is a language guarantee, > not a CPython implementation detail. > > ChrisA Thanks. Informed. The implementation dependent thing is "id(obj)" == virtual mem addr (which caused my bad feeling). Apparently have nothing to do with it here, though. And 'id' is seemingly great to differentiate each obj. -- 吾輩は猫である。ホームーページはhttp://introo.me。