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


Groups > comp.lang.python > #8141

Re: new string-formatting preferred? (was "What is this syntax ?")

Date 2011-06-21 18:02 -0500
From Tim Chase <python.list@tim.thechases.com>
Subject Re: new string-formatting preferred? (was "What is this syntax ?")
References (1 earlier) <87pqm8qh7m.fsf@benfinney.id.au> <4DFFE9D5.30300@tim.thechases.com> <itouv1$jf5$1@dough.gmane.org> <4E008179.600@tim.thechases.com> <itr5de$juc$1@dough.gmane.org>
Newsgroups comp.lang.python
Message-ID <mailman.249.1308697338.1164.python-list@python.org> (permalink)

Show all headers | View raw


On 06/21/2011 05:19 PM, Terry Reedy wrote:
> On 6/21/2011 7:33 AM, Tim Chase wrote:
>>>>> <URL:http://docs.python.org/library/stdtypes.html#str.format>
>>>>
>>>> Is there a good link to a thread-archive on when/why/how .format(...)
>>>> became "preferred to the % formatting"?
>>>
>>> That is a controversial statement.
>>
>> I'm not sure whether you're "controversial" refers to
>>
>> - the documentation at that link,
>
> I meant the preceding statement (derived from the linked source, but
> that is not important) that .format is preferred to %.

I guess then with all the contention, having such a vocal 
preference in the docs (even if tempered by "in new code") seems 
to unnecessarily polarize when I find myself very "meh" in either 
direction.

>>> multi_warn = '''\
>>> Warning: testing multiple {0}s against an iterator will only test
>>> the first {0} unless the iterator is reiterable; most are not.'''.format
>>> ...
>>> print(multiwarn('function'))
>>> ...
>>> print(multiwarn('iterator'))
>>
>> Does the gotcha of a non-restarting iterator
>
> Huh? What iterator?

Your string-body text warns about behavior regarding iterators. 
I was thrown by your warning.

>> The other new feature I saw was the use of __format__()
>
> Suppose one had a Money class with currency and decimal amount fields.
> .__str__ can add a currency symbol (before or after as appropriate) but
> has to use a standard format for the amount field. .__float__ can be
> post-processed according to a %...f spec, but cannot include a currency
> symbol. Money.__format__(self,spec) can format the amount at it wishes,
> including its rounding rules, *and* add a currency symbol.

A Money class was one of the first things I thought of, but 
figured that would be better relegated to an i18n wrapper if you 
wanted it.  Such a wrapper would handle currency-symbol choice & 
positioning positioning (before vs. after; relation to the +/-; 
optional characters for thousands-separators and decimal 
separators; and partitioning at thousands-or-other-multiples, etc).

> Or suppose one has a multi-precision float. %80.40f will require an mpf
> instance to appoximate itself as a float, possibly with error.
> mpg.__format__ should be able to do better.

This case makes a better argument, showing me some new value 
added by __format__().

-tkc


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


Thread

Re: What is this syntax ? Claudiu Popa <cpopa@bitdefender.com> - 2011-06-20 10:18 +0300
  Re: What is this syntax ? Ben Finney <ben+python@benfinney.id.au> - 2011-06-21 08:19 +1000
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-20 19:46 -0500
    Re: new string-formatting preferred? (was "What is this syntax ?") Terry Reedy <tjreedy@udel.edu> - 2011-06-20 22:17 -0400
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-21 06:33 -0500
    Re: new string-formatting preferred? (was "What is this syntax ?") Terry Reedy <tjreedy@udel.edu> - 2011-06-21 18:19 -0400
    Re: new string-formatting preferred? (was "What is this syntax ?") Tim Chase <python.list@tim.thechases.com> - 2011-06-21 18:02 -0500

csiph-web