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

Rhodri James rhodri at kynesim.co.uk
Thu May 10 07:02:19 EDT 2018

On 09/05/18 20:56, Facundo Batista wrote:
> 2018-05-09 13:48 GMT-03:00 Rhodri James <rhodri at kynesim.co.uk>:
>> -1 until you give me an actual spec rather than a curious example.
>> Sorry if that sounds a bit rude, but I spend most of my time trying to find
> Be sorry, it was rude.

On reflection, I'm not sorry at all.  It needed to be said, and 
attempting to deduce a spec from a single example has already caused 
people to go off in a variety of different directions.  If provoking you 
into being even a bit more specific gets our collective cat herd moving 
in something more like the same direction, it'll be worth it.

In that spirit, and trying not to take this as a challenge to see how 
rude I can be without actually being rude: ;-)

I too didn't equate "j" with "join".  I was somewhat expecting "l" for 
"list" given that "s" for "sequence" is taken.  Should I take it that 
everything from the colon to the "j" is an implicitly delimited string 
to use for joining?

If so, does anything in that string need escaping, like say braces or 
other "j"s?  I'm not sure if format specifiers currently allow arbitrary 
text (I don't think they do, but I'm not sure), which might make this a 
more serious undertaking than it first looks.  Alternatively, are 
certain characters forbidden in this implicit join string?

I presume also that no other formatting is possible with this specifier: 
no field widths, no justification, none of the other stuff applicable to 
strings.  I'm not talking about formatting the original strings (or 
whatever) in the list, that would clearly involve crazy amounts of 
sub-formatting and look like a complete mess in the code, but I can see 
a need for formatting the final joined string, and I can't see a way of 
doing that from what you've given us.

I'm not convinced, I admit.  Your use case

 >>> print("{title!r} (${price}) by {authors:, j}".format(**info))
"A book" ($2.34) by John, Mary, Estela

is moderately compelling, but is starting to look too magic for my 
tastes.  I think it's the implicitly delimited join string; it isn't 
obvious at first sight that the comma and space aren't independently 
meaningful in some obscure way, like everything else in that position in 
a format would be.

> This list is for throwing ideas and see if they gain momentum... if
> yes, it will be time for a PEP.
> I know how to do a PEP, believe me.
> But if we'll be asking for a Spec for every idea we think may be
> valuable, we'll stall.

Raising the barrier a bit is no bad thing in my opinion; it shouldn't be 
easy to add things to Python, otherwise we'd have a much uglier 
language.  Regardless, there is a difference between a full-blown PEP 
and an outline spec.  Much as I would like to have every jot and tittle 
in place for a first draft, I know that's not realistic and I don't 
expect it.  On the other hand, bitter experience has taught me that when 
the first draft of a proposal is as nebulous as what you gave us, I 
should walk away from the incipient disaster right now.

Rhodri James *-* Kynesim Ltd

More information about the Python-ideas mailing list