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


Groups > comp.lang.python > #69231

Re: YADTR (Yet Another DateTime Rant)

Path csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed3a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'programmer': 0.03; 'model,': 0.05; 'debugging': 0.07; 'extent': 0.07; 'nicely': 0.07; 'returned.': 0.07; 'string': 0.09; 'bug.': 0.09; 'defines': 0.09; 'expense': 0.09; 'indeed,': 0.09; 'objects,': 0.09; 'portions': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'used.': 0.09; 'violates': 0.09; 'python': 0.11; 'bug': 0.12; "wouldn't": 0.14; '(given': 0.16; 'compute': 0.16; 'finney': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'roy': 0.16; 'say.': 0.16; 'simplest': 0.16; 'unhelpful': 0.16; ':-)': 0.16; 'appropriate': 0.16; 'wrote:': 0.18; 'possible,': 0.19; "python's": 0.19; 'thu,': 0.19; 'separate': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; '(by': 0.24; 'convenient': 0.24; 'fixed.': 0.24; 'defined': 0.27; 'header:X-Complaints-To:1': 0.27; "i'm": 0.30; '(which': 0.31; 'calculated': 0.31; 'concise': 0.31; "d'aprano": 0.31; 'follows': 0.31; 'object.': 0.31; 'steven': 0.31; "user's": 0.31; 'writes:': 0.31; 'url:python': 0.33; 'could': 0.34; 'subject: (': 0.35; 'objects': 0.35; 'there': 0.35; 'leads': 0.36; 'like,': 0.36; 'object,': 0.36; 'returning': 0.36; 'method': 0.36; 'url:org': 0.36; 'should': 0.36; 'half': 0.37; 'so,': 0.37; 'easily': 0.37; 'skip:o 20': 0.38; 'ben': 0.38; 'easiest': 0.38; 'to:addr:python-list': 0.38; 'expect': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'easy': 0.60; 'expression': 0.60; 'negative': 0.60; 'url:3': 0.61; 'simply': 0.61; "you're": 0.61; 'such': 0.63; 'more': 0.64; 'situation': 0.65; 'skip:\xe2 10': 0.65; 'mar': 0.68; 'smith': 0.68; 'results': 0.69; 'default': 0.69; '8bit%:43': 0.74; 'unusual': 0.74; 'behavior': 0.77; 'article': 0.77; '8bit%:46': 0.78; 'real-life': 0.84; 'received:125': 0.84; 'terrible': 0.84; 'url:datamodel': 0.84; 'url:html#object': 0.84; 'url:reference': 0.84; 'skip:\xe2 30': 0.91; 'wanting': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Ben Finney <ben+python@benfinney.id.au>
Subject Re: YADTR (Yet Another DateTime Rant)
Date Fri, 28 Mar 2014 11:43:17 +1100
References <roy-142548.20582725032014@news.panix.com> <mailman.8593.1395876355.18130.python-list@python.org> <53336df8$0$29994$c3e8da3$5496439d@news.astraweb.com> <lh0u51$dj8$1@news.albasani.net> <mailman.8613.1395917059.18130.python-list@python.org> <roy-E22E44.08522427032014@news.panix.com> <5334b747$0$29994$c3e8da3$5496439d@news.astraweb.com> <roy-E6F0E7.20291727032014@news.panix.com>
Mime-Version 1.0
Content-Type text/plain; charset=utf-8
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host jigong.madmonks.org
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 <bignose+hates-spam@benfinney.id.au>
User-Agent Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux)
Cancel-Lock sha1:B2UyewxpCgqY5P+XkgHSANgTxmA=
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.8643.1395967408.18130.python-list@python.org> (permalink)
Lines 76
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1395967408 news.xs4all.nl 2901 [2001:888:2000:d::a6]:34891
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:69231

Show key headers only | View raw


Roy Smith <roy@panix.com> writes:

> In article <5334b747$0$29994$c3e8da3$5496439d@news.astraweb.com>,
>  Steven D'Aprano <steve+comp.lang.python@pearwood.info> wrote:
>
> > On Thu, 27 Mar 2014 08:52:24 -0400, Roy Smith wrote:
> > > Give ma a real-life situation where you would want such behavior
> > > [the ‘datetime.timedelta.__str__’ method returning a string
> > > showing some portions negative and others positive].
> > 
> > Easy -- I'm debugging timedelta routines, and I want to easily see
> > that the timedeltas calculated match what I expect them to be when I
> > print them. The quickest, easiest and simplest way is for
> > str(timedelta) to follow the internal representation.
>
> That's what __repr__() is for.

Indeed, that is why Python's data model defines separate ‘__str__’ and
‘__repr__’ methods.

    object.__repr__(self)

    […] If at all possible, this should look like a valid Python
    expression that could be used to recreate an object with the same
    value (given an appropriate environment). If this is not possible, a
    string of the form <...some useful description...> should be
    returned.

    <URL:http://docs.python.org/3/reference/datamodel.html#object.__repr__>

    object.__str__(self)

    […] compute the “informal” or nicely printable string representation
    of an object. […] a more convenient or concise representation can be
    used.

    <URL:http://docs.python.org/3/reference/datamodel.html#object.__str__>

If you're a programmer wanting to know about the internal representation
of an object, call its ‘__repr__’ method (by calling ‘repr(foo)’).

If you're wanting to see a string representation that follows the
*semantics* of the object, call its ‘__str__’ method (by calling
‘str(foo)’).

If the ‘__str__’ method follows the internal representation at the
expense of the user's conceptual model, that's terrible (which I think
is what Roy means by “braindead”).

I wouldn't go as far as that insult, because it could be mere omission:

    The default implementation [of ‘__str__’] defined by the built-in
    type object calls object.__repr__().

So, where ‘str(foo)’ is returning an unhelpful representation that
follows the implementation, it could simply be that there is no
‘__str__’ defined for that type.

> > Oh look, that's exactly what the docs say:
> > 
> > "String representations of timedelta objects are normalized
> > similarly to their internal representation. This leads to somewhat
> > unusual results for negative timedeltas."
>
> Yes, I know that's what the docs say.  That's why it's not an 
> implementation bug.  It's a design bug :-)

One which to some extent violates the defined data model for Python
objects, and hence should be fixed.

-- 
 \      “I don't know half of you half as well as I should like, and I |
  `\   like less than half of you half as well as you deserve.” —Bilbo |
_o__)                                                          Baggins |
Ben Finney

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


Thread

YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-25 20:58 -0400
  Re: YADTR (Yet Another DateTime Rant) Ethan Furman <ethan@stoneleaf.us> - 2014-03-25 18:19 -0700
  Re: YADTR (Yet Another DateTime Rant) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-26 09:27 +0000
  Re: YADTR (Yet Another DateTime Rant) Skip Montanaro <skip@pobox.com> - 2014-03-26 08:37 -0500
    Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-26 08:04 -0700
      Re: YADTR (Yet Another DateTime Rant) Skip Montanaro <skip@pobox.com> - 2014-03-26 10:39 -0500
        Re: YADTR (Yet Another DateTime Rant) Marko Rauhamaa <marko@pacujo.net> - 2014-03-26 17:58 +0200
          Re: YADTR (Yet Another DateTime Rant) Skip Montanaro <skip@pobox.com> - 2014-03-26 11:34 -0500
            Re: YADTR (Yet Another DateTime Rant) Marko Rauhamaa <marko@pacujo.net> - 2014-03-26 19:13 +0200
            Re: YADTR (Yet Another DateTime Rant) Grant Edwards <invalid@invalid.invalid> - 2014-03-26 19:12 +0000
              Re: YADTR (Yet Another DateTime Rant) Ben Finney <ben+python@benfinney.id.au> - 2014-03-27 07:06 +1100
      Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-26 11:50 -0400
  Re: YADTR (Yet Another DateTime Rant) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-26 15:02 +0100
    Re: YADTR (Yet Another DateTime Rant) Marko Rauhamaa <marko@pacujo.net> - 2014-03-26 16:43 +0200
  Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-27 01:12 +1100
    Re: YADTR (Yet Another DateTime Rant) Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-03-27 11:08 +1300
  Re: YADTR (Yet Another DateTime Rant) Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-03-26 19:25 -0400
    Re: YADTR (Yet Another DateTime Rant) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-27 00:16 +0000
      Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-27 12:28 +1100
        Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-26 21:38 -0400
          Re: YADTR (Yet Another DateTime Rant) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-27 08:40 +0000
      Re: YADTR (Yet Another DateTime Rant) Dan Sommers <dan@tombstonezero.net> - 2014-03-27 03:56 +0000
      Re: YADTR (Yet Another DateTime Rant) Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-27 11:22 +0100
        Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-27 21:44 +1100
          Re: YADTR (Yet Another DateTime Rant) Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-27 12:05 +0100
            Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-27 22:33 +1100
          Re: YADTR (Yet Another DateTime Rant) Johannes Bauer <dfnsonfsduifb@gmx.de> - 2014-03-27 12:25 +0100
            Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-27 22:42 +1100
            Re: YADTR (Yet Another DateTime Rant) Skip Montanaro <skip@pobox.com> - 2014-03-27 06:56 -0500
              Re: YADTR (Yet Another DateTime Rant) Marko Rauhamaa <marko@pacujo.net> - 2014-03-27 16:04 +0200
            Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-27 23:20 +1100
            Re: YADTR (Yet Another DateTime Rant) Skip Montanaro <skip@pobox.com> - 2014-03-27 08:01 -0500
            Re: YADTR (Yet Another DateTime Rant) Terry Reedy <tjreedy@udel.edu> - 2014-03-27 17:10 -0400
            Re: YADTR (Yet Another DateTime Rant) Ethan Furman <ethan@stoneleaf.us> - 2014-03-27 17:04 -0700
            Re: YADTR (Yet Another DateTime Rant) Terry Reedy <tjreedy@udel.edu> - 2014-03-27 21:46 -0400
              Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-27 21:59 -0400
                Re: YADTR (Yet Another DateTime Rant) Terry Reedy <tjreedy@udel.edu> - 2014-03-27 22:24 -0400
                Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-28 14:03 +1100
          Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-27 08:52 -0400
            Re: YADTR (Yet Another DateTime Rant) Antoon Pardon <antoon.pardon@rece.vub.ac.be> - 2014-03-27 15:12 +0100
            Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-28 01:15 +1100
            Re: YADTR (Yet Another DateTime Rant) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-27 23:41 +0000
              Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-27 20:29 -0400
                Re: YADTR (Yet Another DateTime Rant) Ben Finney <ben+python@benfinney.id.au> - 2014-03-28 11:43 +1100
                Re: YADTR (Yet Another DateTime Rant) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-28 11:11 +0000
                Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-28 08:30 -0400
                Re: YADTR (Yet Another DateTime Rant) Marko Rauhamaa <marko@pacujo.net> - 2014-03-28 15:10 +0200
                Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-29 00:19 +1100
                Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-28 09:22 -0400
                Re: YADTR (Yet Another DateTime Rant) Rustom Mody <rustompmody@gmail.com> - 2014-03-28 06:30 -0700
                Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-29 00:32 +1100
                Re: YADTR (Yet Another DateTime Rant) Roy Smith <roy@panix.com> - 2014-03-28 09:20 -0400
                Re: YADTR (Yet Another DateTime Rant) Marko Rauhamaa <marko@pacujo.net> - 2014-03-28 16:34 +0200
                Re: YADTR (Yet Another DateTime Rant) Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-03-28 14:35 +0000
                Re: YADTR (Yet Another DateTime Rant) Chris Angelico <rosuav@gmail.com> - 2014-03-29 02:25 +1100
  Re: YADTR (Yet Another DateTime Rant) Ethan Furman <ethan@stoneleaf.us> - 2014-03-26 16:33 -0700

csiph-web