Path: csiph.com!usenet.pasdenom.info!news.albasani.net!newsfeed.freenet.ag!news2.euro.net!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.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'argument': 0.04; 'subject:Python': 0.05; 'formatting': 0.07; 'wrapper': 0.07; '0),': 0.09; '22,': 0.09; 'braces': 0.09; 'docs.': 0.09; 'method:': 0.09; 'spec': 0.09; 'specifiers': 0.09; 'def': 0.10; "skip:' 30": 0.15; 'value.': 0.15; '"format': 0.16; "'hello": 0.16; 'colon.': 0.16; 'curly': 0.16; 'invoking': 0.16; 'overriding': 0.16; 'reversed': 0.16; 'skip:) 30': 0.16; 'spec):': 0.16; 'specifier': 0.16; 'string:': 0.16; 'string': 0.17; 'wrote:': 0.17; 'documented': 0.17; 'skip:{ 20': 0.17; 'string,': 0.17; 'thu,': 0.17; '>>>': 0.18; 'module': 0.19; 'skip:p 30': 0.20; '(b)': 0.22; 'latter': 0.22; 'example': 0.23; 'seems': 0.23; 'raise': 0.24; 'second': 0.24; 'header:In-Reply-To:1': 0.25; '(which': 0.26; 'values': 0.26; 'am,': 0.27; 'rules': 0.27; 'message-id:@mail.gmail.com': 0.27; 'optional': 0.29; 'skip:_ 10': 0.29; 'class': 0.29; "skip:' 10": 0.30; 'basic': 0.30; 'code': 0.31; 'implement': 0.32; 'anywhere': 0.33; 'goes': 0.33; '(a)': 0.33; '11,': 0.33; 'to:addr:python-list': 0.33; '(c)': 0.33; 'received:google.com': 0.34; 'third': 0.34; 'built-in': 0.35; 'fail': 0.35; 'nov': 0.35; 'received:209.85': 0.35; 'there': 0.35; 'but': 0.36; 'skip:{ 10': 0.36; 'should': 0.36; 'skip:p 20': 0.36; 'beyond': 0.37; 'does': 0.37; 'two': 0.37; 'being': 0.37; 'passed': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'object': 0.38; 'to:addr:python.org': 0.39; 'header:Received:5': 0.40; 'skip:u 10': 0.60; 'world': 0.63; 'more': 0.63; 'here': 0.65; 'self.value': 0.84; 'to:name:python': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=QB9lD3YS8MNQEDOu5WYkJw66gG7ubyg/J21pldI1xzs=; b=Vpxon8mGVNs0pOcIn+lR4nORYk4c58gz0feedyiAYM3i4gALcI/4/8AVaWi3BctVMc +JRLXkLMBQns/Ax56sqgZyzTEjF2NpKSYX8/Wbag31YU9zLs16KU5UY8AV/ZAURCb+Mt 7R5YvZKPe48oouVXgVaMY/4vEKom3syyWT/FmcA1Pgp3KwijCtw0GRrHMS2f05fFLm5Q EMHdsv8j5RpP8DMoq3NzgXfXQJCg9xTYA/w/TYIU4u4vOi5HTCkH4DZYjzGhFFhaXGg1 pyc3aeKKAmQoYeEm9a1ulhbwLohKa85gS1g392ZbGuU6rKVxdBaUFDqz7LScdTwoAEXh ckfg== MIME-Version: 1.0 In-Reply-To: <50AE1986.2080605@ncf.ca> References: <31a82817-8c9b-4dd2-a468-89d8d081fd1b@googlegroups.com> <50AD0962.5080002@ncf.ca> <50AE1986.2080605@ncf.ca> From: Ian Kelly Date: Thu, 22 Nov 2012 11:27:10 -0700 Subject: Re: Yet another Python textbook To: Python Content-Type: text/plain; charset=ISO-8859-1 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: 71 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1353608868 news.xs4all.nl 6975 [2001:888:2000:d::a6]:44206 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:33812 On Thu, Nov 22, 2012 at 5:24 AM, Colin J. Williams wrote: > From my reading of the docs, it seems to me that the three following should > be equivalent: > > (a) formattingStr.format(values) > with > (b) format(values, formattingStr) > or > (c) tupleOfValues.__format__(formattingStr > > Example: > print('{:-^14f}{:^14d}'.format(-25.61, 95 )) > print(format((-25.61, 95), '{:-^14f}{:^14d}')) > (-25.61, 95 ).__format__('{:-^14f}{:^14d}') > > The second fails, perhaps because values can only be a single value. > The third fails, the reason is unclear. The latter two (which are more or less equivalent) fail because they are intended for invoking the formatting rules of a single value. The string argument to each of them is not a format string, but a "format specification", which in a format string is only the part that goes inside the curly braces and after the optional colon. For example, in this format string: >>> 'Hello world {0!s:_>4s}'.format(42) 'Hello world __42' The format specifier here is "_>4s": >>> format('42', '_>4s') '__42' The valid format specifiers depend upon the type of the object being formatted: >>> format(42, '04x') '002a' >>> format(datetime(2012, 11, 22, 11, 17, 0), 'The time is %Y %d %m %H:%M:%S') 'The time is 2012 22 11 11:17:00' Custom types can implement custom format specifications by overriding the __format__ method: >>> class Foo: ... def __init__(self, value): ... self.value = value ... def __format__(self, spec): ... if spec == 'a': ... return str(self.value) ... if spec == 'b': ... return ''.join(reversed(str(self.value))) ... raise ValueError("Unknown format code {!r}".format(spec)) ... >>> format(Foo(42), 'a') '42' >>> format(Foo(42), 'b') '24' The same format specifications can then also be passed to str.format: >>> '{0:a} reversed is {0:b}'.format(Foo(42)) '42 reversed is 24' Unfortunately, there does not seem to be a good reference to the format specifications available for built-in types beyond basic strings and numbers. I only knew about the datetime example because it is used in an example in the str.format docs. The datetime.__format__ implementation (which seems to be just a thin wrapper of datetime.strftime) does not seem to be documented anywhere in the datetime module docs.