[Python-ideas] Have a "j" format option for lists

Eric V. Smith eric at trueblade.com
Thu May 10 15:56:31 EDT 2018


On 5/10/18 12:28 PM, Facundo Batista wrote:
> 2018-05-10 10:34 GMT-03:00 Chris Angelico <rosuav at gmail.com>:
>
>>>
>>> Ideally, it will handle *any* iterable.
>>
>> If it's to handle arbitrary iterables, it can't be the normal style of
>> "take this string, pass it to the object's __format__ method, and let
>> it interpret it". That's why I suggested a bang notation instead. We
>> have some already:
>
> Yes, I think it fits better, as it's not just a formatting, it's more
> an "operation"...
>
>>  A "!j" flag
>> could take an iterable, format each element using the given format,
>> and then join them. The letter "j" makes good sense then, as it
>
> Where would you indicate the separator if we use the bang notation?

You would have to wedge it in before the colon. Something like:

f'{lst!j, :20}'

But then you couldn't have a colon in the separator. And this would be 
our first conversion character to be more than a single character.

I'm opposed to this proposal. Just call str.join(), or write a helper 
function or class (depending on what you're trying to do). It's not 
worth complicating what's already a complicated topic.

Eric


More information about the Python-ideas mailing list