Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8138
| Path | csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!selfless.tophat.at!newsfeed.xs4all.nl!newsfeed6.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.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'guido': 0.04; 'instance': 0.05; 'int': 0.05; 'suppose': 0.05; 'dictionary': 0.07; 'formatting': 0.07; 'int,': 0.07; 'refers': 0.07; 'terry': 0.07; 'python': 0.08; '%s"': 0.09; '*and*': 0.09; '3.0,': 0.09; '>>>>': 0.09; 'float.': 0.09; 'instance.': 0.09; 'operator,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'restriction': 0.09; 'rounding': 0.09; 'similarly,': 0.09; 'statement.': 0.09; 'str,': 0.09; 'subject:string': 0.09; 'pm,': 0.10; '>>>': 0.12; 'am,': 0.14; 'broken': 0.14; 'described': 0.14; 'wrote:': 0.14; '(before': 0.16; 'appropriate)': 0.16; 'deprecation': 0.16; 'finney': 0.16; 'iterator': 0.16; 'reedy': 0.16; 'spec,': 0.16; 'subject:formatting': 0.16; 'subject:syntax': 0.16; 'url:format': 0.16; 'weighed': 0.16; 'wins': 0.16; '{0}': 0.16; 'float': 0.16; 'subject:was': 0.16; 'possibly': 0.16; 'meant': 0.18; 'jan': 0.20; 'header:In-Reply-To:1': 0.21; "haven't": 0.22; '(but': 0.22; 'compared': 0.22; 'stuff': 0.22; 'currency': 0.23; 'indicating': 0.23; 'once.': 0.23; 'objects': 0.23; 'code': 0.24; 'there.': 0.25; 'function': 0.25; 'preferred': 0.26; 'statement': 0.26; 'string': 0.26; 'pass': 0.27; 'says': 0.27; 'testing': 0.27; 'example': 0.27; "i'm": 0.27; 'explicitly': 0.29; 'class': 0.29; 'version': 0.29; 'bit': 0.30; 'least': 0.30; 'chase': 0.30; 'clear,': 0.30; 'decimal': 0.30; 'exist.': 0.30; 'preceding': 0.30; 'style.': 0.30; 'tuples': 0.30; 'developers': 0.30; 'it.': 0.31; 'url:library': 0.31; 'named': 0.32; 'yet': 0.32; 'header:X-Complaints-To:1': 0.32; 'does': 0.33; 'to:addr :python-list': 0.33; 'post': 0.33; 'operations': 0.33; '...': 0.34; 'source': 0.34; 'there': 0.35; 'header:User-Agent:1': 0.35; 'skip:" 10': 0.35; '"we': 0.35; 'doc': 0.35; 'duplicate': 0.35; 'skip:{ 10': 0.35; 'source,': 0.35; 'subject:What': 0.35; 'symbol': 0.35; 'wishes,': 0.35; 'yet,': 0.35; 'couple': 0.35; 'using': 0.35; 'test': 0.35; 'probably': 0.36; 'subject:new': 0.37; 'instead.': 0.37; 'parallel': 0.37; 'url:docs': 0.37; 'case': 0.37; 'url:python': 0.38; 'received:org': 0.38; 'url:org': 0.38; 'but': 0.38; 'though': 0.38; 'linked': 0.38; 'menu': 0.38; 'subject:: ': 0.38; 'doing': 0.39; 'should': 0.39; 'unless': 0.39; 'easier': 0.39; 'perhaps': 0.39; 'according': 0.63; 'link': 0.64; 'saw': 0.64; 'harder': 0.65; 'exact': 0.65; 'here': 0.66; 'special': 0.66; 'became': 0.67; 'benefit': 0.70; 'subject:this': 0.76; 'money': 0.78; 'flexible,': 0.84; 'gotcha': 0.84; 'huh?': 0.84; 'prefers': 0.84; '\xe2\x80\x9cthis': 0.84; 'direction,': 0.91; 'quotation': 0.91; 'spec': 0.91 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Terry Reedy <tjreedy@udel.edu> |
| Subject | Re: new string-formatting preferred? (was "What is this syntax ?") |
| Date | Tue, 21 Jun 2011 18:19:26 -0400 |
| References | <mailman.178.1308554725.1164.python-list@python.org> <87pqm8qh7m.fsf@benfinney.id.au> <4DFFE9D5.30300@tim.thechases.com> <itouv1$jf5$1@dough.gmane.org> <4E008179.600@tim.thechases.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=UTF-8; format=flowed |
| Content-Transfer-Encoding | quoted-printable |
| X-Gmane-NNTP-Posting-Host | rain.gmane.org |
| User-Agent | Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.17) Gecko/20110414 Lightning/1.0b2 Thunderbird/3.1.10 |
| In-Reply-To | <4E008179.600@tim.thechases.com> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.12 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://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 | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.246.1308694779.1164.python-list@python.org> (permalink) |
| Lines | 149 |
| NNTP-Posting-Host | 82.94.164.166 |
| X-Trace | 1308694780 news.xs4all.nl 49184 [::ffff:82.94.164.166]:48670 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | x330-a1.tempe.blueboxinc.net comp.lang.python:8138 |
Show key headers only | View raw
On 6/21/2011 7:33 AM, Tim Chase wrote:
> On 06/20/2011 09:17 PM, Terry Reedy wrote:
>> On 6/20/2011 8:46 PM, Tim Chase wrote:
>>> On 06/20/2011 05:19 PM, Ben Finney wrote:
>>>> “This method of string formatting is the new standard in
>>>> Python 3.0, and should be preferred to the % formatting
>>>> described in String Formatting Operations in new code.”
>>>>
>>>> <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,
> - Ben's quote of the documentation at that link,
> - my quotation of Ben's quote of the documentation,
> - or my request for a "thread-archive on the when/why/how"
>
> I _suspect_ you mean the first one :)
I meant the preceding statement (derived from the linked source, but
that is not important) that .format is preferred to %. Guido prefers it.
I prefer it. At least a couple of developers vocally do not prefer it
and might prefer that the statement was not there. Guido recognizes that
deprecation of % formatting would at least require a conversion function
that does not now exist.
I see that the linked doc says 'in new code'. That makes the statement
less (but only less) controversial.
>
>>> I haven't seen any great wins of the new formatting over
>>> the classic style.
>>
>> It does not abuse the '%' operator,
>
> Weighed against the inertia of existing code/documentation/tutorials, I
> consider this a toss-up. If .format() had been the preferred way since
> day#1, I'd grouse about adding/overloading '%', but going the other
> direction, there's such a large corpus of stuff using '%', the addition
> of .format() feels a bit schizophrenic.
>
>> it does not make a special case of tuples (a source of bugs),
>
> Having been stung occasionaly by this, I can see the benefit here over
> writing the less-blatant
>
> "whatever %s" % (tupleish,)
>
>> and it is more flexible, especially
>> indicating objects to be printed. Here is a simple example from my code
>> that would be a bit more difficult with %.
>>
>> 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?
> trump pulling each field you want and passing it explicitly?
Huh? I explicitly pass the strings to be printed.
> In pre-.format(), I'd just use dictionary formatting:
>
> "we have %(food)s & eggs and %(food)s, bacon & eggs" % {
> "food": "spam", # or my_iterator.next()?
> }
A better parallel to my example would be
menu = "We have %(meat)s & eggs or %(meat)s and potatoes."
print(menu % {'meat':'spam'})
print(menu % {'meat':'ham'})
The exact duplicate of that with .format is
menu = "We have {meat} & eggs or {meat} and potatoes.".format
print(menu(meat = 'spam'))
print(menu(meat = 'ham'))
One knock against '.format' is that it is 6 chars more that '%'. But for
repeat usage, it is only needed once. And look: '%(meat)s' is 2 more
chars than '{meat}' and, to me, {} is easier to type than (). Then " %
{'meat':"spam"}" is 3 more chars than "(meat = 'ham')" and definitely
harder to type. While I prefer '}' to ')', I prefer '))' to the mixed
'})'. The % way is at least 'a bit more difficult' even compared to the
longer and harder .format with named fields.
menu = "We have {0} & eggs or {0} and potatoes.".format
print(menu('spam'))
print(menu('ham'))
it a little easier yet, though perhaps less clear, especially if there
were multiple substitutions.
> The other new feature I saw was the use of __format__() which may have
> good use-cases, but I don't yet have a good example of when I'd want
> per-stringification formatting compared to just doing my desired
> formatting in __str__() instead.
__str__ always returns the same string for an instance in a given state.
Similarly, __float__ and __int__ will return the same float or int
version of an unchanged instance. __format__(spec) can directly adjust
the result according to spec without the restriction of going through an
intermediary str, int, or float.
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.
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.
(Sadly, this new ability to more accurately represent objects is not yet
used for ints and is broken for fractions.Fraction. I will probably post
issues on the tracker.)
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll 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