syntax difference
Chris Angelico
rosuav at gmail.com
Sun Jun 17 02:32:25 EDT 2018
On Sun, Jun 17, 2018 at 3:56 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> Chris Angelico <rosuav at gmail.com> writes:
>
>> On Sun, Jun 17, 2018 at 3:30 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
>> > (or, if you want to continue with the older less-flexible style,
>
> (I gave an unhelpful URL for that documentation. Try this instead
> <URL:https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting>.)
>
>> For the record, there's nothing at all wrong with printf-style
>> formatting; its flexibility and brevity make it extremely useful in
>> many situations.
>
> That isn't the impression I get from the above documentation. It starts
> with a clearly worded warning:
>
> Note
>
> The formatting operations described here exhibit a variety of quirks
> that lead to a number of common errors (such as failing to display
> tuples and dictionaries correctly). Using the newer formatted string
> literals or the str.format() interface helps avoid these errors.
> These alternatives also provide more powerful, flexible and
> extensible approaches to formatting text.
>
> That states a clear opinion that ‘str.format’ is preferred.
And yet we have had a firm statement that percent-formatting is not
going to be deprecated. Which is a clear statement that there's
nothing wrong with it. The only problem I'm aware of with tuples and
dicts is that a single parameter is misinterpreted, which is a minor
nit caused by the operator syntax; the format mini-language itself is
not affected by this. For instance, you can dodge the single-argument
problem thus:
>>> def sprintf(fmt, *args):
... return fmt % args
...
>>> sprintf("%s", (1, 2, 3))
'(1, 2, 3)'
Also: I would call that a "note", not a "warning". Please stop
spreading the FUD that there's somehow something "wrong" with using
what is a well-known convention for a format mini-language.
ChrisA
More information about the Python-list
mailing list