Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #81710

Re: Can you use self in __str__

Path csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed4.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'python,': 0.02; 'cpython': 0.05; 'messages.': 0.05; 'exist,': 0.09; 'namespace': 0.09; 'parsing': 0.09; 'cc:addr:python-list': 0.11; 'language,': 0.12; '(note': 0.16; 'allocates': 0.16; 'dict': 0.16; 'enough.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'storing': 0.16; 'sat,': 0.16; 'language': 0.16; 'wrote:': 0.18; 'dependent': 0.19; 'finished': 0.19; 'first.': 0.19; 'later': 0.20; '>>>': 0.22; '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; 'function': 0.29; 'chris': 0.29; 'see,': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'lines': 0.31; 'continues': 0.31; 'object.': 0.31; 'produces': 0.31; 'another': 0.32; 'beginning': 0.33; 'fri,': 0.33; 'used,': 0.33; '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; 'clear': 0.37; 'easily': 0.37; 'being': 0.38; 'sometimes': 0.38; 'server': 0.38; 'nov': 0.38; 'same.': 0.38; 'stable': 0.38; 'pm,': 0.38; 'sure': 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; 'mailbox': 0.68; 'compliant': 0.68; 'detail.': 0.68; 'guaranteed': 0.75; 'believe,': 0.84; 'subject:self': 0.84; 'subject:you': 0.87; 'destination': 0.91; 'to:none': 0.92; 'imagine': 0.93
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=FId/bFR3yv4LnbErOpT+2tHSdZlgc1z6yqKVbsxKf7A=; b=vuo27vjmmz1FcNUMrnSP6Hv33RgnQ+7XUQVPpBwhFh5Tu7bnlHY3Qz84VxtjIH56Nc qSxqW6yq6fA8vtBxxkkDR1I/rBIxt7STCkNxoCI25yotD+vQhgUy80Fnv/Udypf87egc 8QS8tsZtdT1ES0Cu9qqgiuY4FdbFuFJ6Ym4XR1pCON6acd2dJWPbgxDNxHD2Ffq9YTsO MysYEcZPAufQ0MpY17WGHSwM+2EwJbgLW4lhGffGeAmg2vmPRFKvdUdjpJRQmohEdNdm aQffFmKKK4Ynmb1hYtUDcNpeRIT/ERuf7eVsqNbpIwBBaQEyLny7UWXg7e+vtYTA8wLc tYCQ==
MIME-Version 1.0
X-Received by 10.107.136.92 with SMTP id k89mr42787015iod.43.1417232207634; Fri, 28 Nov 2014 19:36:47 -0800 (PST)
In-Reply-To <CAJQX3DyLr4W3qJm4npgS2Fd=-6ayQJcpKU+u_rz8765yHwAWsw@mail.gmail.com>
References <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> <CAJQX3DwmK2FedvbVMUq5Mm2yMSfD5QQWz3qqph5oj2_tzZyMvA@mail.gmail.com> <CAPTjJmok2qfERwgyS7PqfFLg7tjYu9koiBHdfQN3DanZ00p1Mw@mail.gmail.com> <CAJQX3DyLr4W3qJm4npgS2Fd=-6ayQJcpKU+u_rz8765yHwAWsw@mail.gmail.com>
Date Sat, 29 Nov 2014 14:36:47 +1100
Subject Re: Can you use self in __str__
From Chris Angelico <rosuav@gmail.com>
Cc Python <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 <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.16423.1417232210.18130.python-list@python.org> (permalink)
Lines 44
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1417232210 news.xs4all.nl 2866 [2001:888:2000:d::a6]:37953
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:81710

Show key headers only | View raw


On Sat, Nov 29, 2014 at 2:16 PM, Shiyao Ma <i@introo.me> wrote:
> 2014-11-28 13:00 GMT+08:00 Chris Angelico <rosuav@gmail.com>:
>> On Fri, Nov 28, 2014 at 2:04 PM, Shiyao Ma <i@introo.me> wrote:
>>> What if it's in the local namespace of a function or method? IDK, try
>>> to get that thing first.
>>
> Sure enough. I will even avoid using "id" as it's dependent on CPython
> implementation. :)

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

Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 20:26 -0500
  Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-27 21:49 -0500
    Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 22:31 -0500
      Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-28 04:37 -0500
  Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-28 11:04 +0800
    Re: Can you use self in __str__ Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-11-27 22:35 -0500
      Re: Can you use self in __str__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-28 18:44 +1100
  Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-28 16:00 +1100
  Re: Can you use self in __str__ Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-11-28 19:25 +1100
  Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-28 12:43 +1100
  Re: Can you use self in __str__ Dave Angel <davea@davea.name> - 2014-11-28 06:43 -0500
  Re: Can you use self in __str__ Rustom Mody <rustompmody@gmail.com> - 2014-11-28 06:21 -0800
    Re: Can you use self in __str__ Rustom Mody <rustompmody@gmail.com> - 2014-11-28 08:37 -0800
  Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-29 11:16 +0800
  Re: Can you use self in __str__ Chris Angelico <rosuav@gmail.com> - 2014-11-29 14:36 +1100
  Re: Can you use self in __str__ Shiyao Ma <i@introo.me> - 2014-11-29 11:55 +0800

csiph-web