On Sun, May 02, 2021 at 04:30:38PM -0400, Eric V. Smith wrote:

[I suggested]
> >Better to invent a new format code, which I'm going to spell as 'X' for
> >lack of something better, that maps a format specifier to every
> >element of any iterable (not just generator comprehensions):
> >
> >     f'{row:X>8.3f} | {sum(row):>8.3f}'
> >
> >meaning, format each element of row with `>8.3f`.
> 
> I'm completely opposed to all of this, but I'll note that you don't want 
> the thing that gives instructions to the f-string mechanism to be part 
> of the format specifier, you'd want it to be a conversion flag like !r 
> and !s. The format spec is reserved for the object being formatted. The 
> way this is specified, you'd either have to change the f-string 
> mechanics to start interpreting format specs, or have every iterable 
> understand what "X" means.

Good point! Thank you.


> For example, "X" is already understood by datetime as part of its format 
> spec "language":
> 
> >>> f'{datetime.now() - timedelta(days=3):X days ago it was a %A}'
> 'X days ago it was a Thursday'
> 
> I don't think you'd want f-strings to hijack that expression because it 
> starts with "X". Better to do something like:
> 
> f'{row!X:>8.3f} | {sum(row):>8.3f}'

That makes more sense.


> But I'll reiterate that I'm opposed. Sometimes you just need to do the 
> formatting outside of an f-string. Terseness shouldn't be the ultimate goal.

I agree with your general observation, but in the specific case of "do 
this to every element of this iterable", I think that's common enough 
that we should consider building it into the mini-languages used by 
format and f-strings.

Python is an extremely readable language, and it is possible to be too 
terse as well as too verbose. Not everything needs to be a one-liner 
single expression. But if you see boilerplate code like:

    for each element of spam:
        format element in the exact same way

it makes sense to consider abstracting it into a single operation:

    format each element of spam


-- 
Steve
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/HBNF74DEQJMHA5GLBKU33XWMY3M4BKXF/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to