[Python-Dev] PEP-498: Literal String Formatting

Paul Moore p.f.moore at gmail.com
Mon Aug 17 12:02:15 CEST 2015


On 17 August 2015 at 05:34, Victor Stinner <victor.stinner at gmail.com> wrote:
> 2015-08-16 7:21 GMT-07:00 Paul Moore <p.f.moore at gmail.com>:
>> 2. By far and away the most common use for me would be things like
>> print(f"Iteration {n}: Took {end-start) seconds"). At the moment I use
>> str,format() for this, and it's annoyingly verbose. This would be a
>> big win, and I'm +1 on the PEP for this specific reason.
>
> You can use a temporary variable, it's not much longer:
>    print("Iteration {n}: Took {dt) seconds".format(n=n, dt=end-start))
> becomes
>    dt = end - start
>    print(f"Iteration {n}: Took {dt) seconds")

... which is significantly shorter (my point). And using an inline expression

    print(f"Iteration {n}: Took {end-start) seconds")

with (IMO) even better readability than the version with a temporary variable.

>
>> 3. All of the complex examples look scary, but in practice I wouldn't
>> write stuff like that - why would anyone do so unless they were being
>> deliberately obscure?
>
> I'm quite sure that users will write complex code in f-strings.

So am I. Some people will always write bad code. I won't (or at least,
I'll try not to write code that *I* consider to be complex :-)) but
"you can use this construct to write bad code" isn't an argument for
dropping the feature. If you couldn't find *good* uses, that would be
different, but that doesn't seem to be the case here (at least in my
view).

Paul.


More information about the Python-Dev mailing list