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

Paul Moore p.f.moore at gmail.com
Wed May 9 10:01:21 EDT 2018


On 9 May 2018 at 14:49, Eric V. Smith <eric at trueblade.com> wrote:
> On 5/9/18 9:28 AM, Chris Angelico wrote:
>> On Wed, May 9, 2018 at 11:06 PM, Steven D'Aprano <steve at pearwood.info>
>> wrote:
>>
>>> - do you truly mean lists *only*, or is any iterable acceptible?
>>
>> With the letter being "j" and the semantics being lifted from
>> str.join(), I would guess the latter.
>
> Since '{:spec}'.format(obj) basically becomes obj.__format__('spec'), this
> would have to be implemented on a concrete type (in the above example,
> list).

[...]

> I would object to changing the format machinery. Any format spec should be
> interpreted by some object's __format__ method.

Agreed. In theory this is a nice idea, but the way formatting is
implemented (and the fact that join is a method on strings taking an
arbitrary iterable as an argument) means that it's a bad fit for the
format mini-language.

I don't think the improved convenience is sufficient to warrant the
change that would be required in practice. (But if someone found a way
to make it work *without* changes to the underlying format machinery,
that would be a different matter...)

Paul


More information about the Python-ideas mailing list