Eric V. Smith writes:
> 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}'
I wondered, is that even possible? OK, I guess you could do it with a
Row(Any) class something like
class Row:
def __init__(self, row):
self.row = row
def __format__(self, spec):
"""
spec looks like "\t/\n/>8.3f"
"""
sep, end, spec = spec.split('/') # random bikeshed,
# paint later
return sep.join(format(self.row, spec)) + end
but why bother defining !X to convert to Row when
f'{Row(row)://>8.3f} | {sum(row):>8.3f}'
should DTRT with the above class? EIBTI here, I think. (OTOH, with
Cameron's idea of a programmable mapping of conversions, you could
give Row a better name like "IterableFormatter", get the benefit of
brevity with a conversion code, and still be explicit enough for me.
YMMV)
> 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.
"Terse enough" looks possible to me, though. Even without a new
conversion.
Steve
_______________________________________________
Python-ideas mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/[email protected]/message/CWBBNLLH76XHTGC2KFAHIDJSG7XAVS67/
Code of Conduct: http://python.org/psf/codeofconduct/