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: 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 Subject: Re: YADTR (Yet Another DateTime Rant) Date: Fri, 28 Mar 2014 11:43:17 +1100 References: <53336df8$0$29994$c3e8da3$5496439d@news.astraweb.com> <5334b747$0$29994$c3e8da3$5496439d@news.astraweb.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 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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: 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 Roy Smith writes: > In article <5334b747$0$29994$c3e8da3$5496439d@news.astraweb.com>, > Steven D'Aprano 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. object.__str__(self) […] compute the “informal” or nicely printable string representation of an object. […] a more convenient or concise representation can be used. 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