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


Groups > comp.lang.python > #81711

Re: Can you use self in __str__

References <9oif7apuou9fbaqjk0i3l2pq1theuso4un@4ax.com> <CAJQX3DwmK2FedvbVMUq5Mm2yMSfD5QQWz3qqph5oj2_tzZyMvA@mail.gmail.com> <CAPTjJmok2qfERwgyS7PqfFLg7tjYu9koiBHdfQN3DanZ00p1Mw@mail.gmail.com> <CAJQX3DyLr4W3qJm4npgS2Fd=-6ayQJcpKU+u_rz8765yHwAWsw@mail.gmail.com> <CAPTjJmr5C8Gr4m3RzOUiftvt_=3QJOXBuWqp5FhLbz=VJeV5CQ@mail.gmail.com>
Date 2014-11-29 11:55 +0800
Subject Re: Can you use self in __str__
From Shiyao Ma <i@introo.me>
Newsgroups comp.lang.python
Message-ID <mailman.16424.1417233725.18130.python-list@python.org> (permalink)

Show all headers | View raw


2014-11-29 11:36 GMT+08:00 Chris Angelico <rosuav@gmail.com>:
> 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。

Back to comp.lang.python | Previous | Next — Previous 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