[Python-ideas] new format spec for iterable types

Andrew Barnert abarnert at yahoo.com
Wed Sep 9 02:03:22 CEST 2015


On Sep 8, 2015, at 12:38, random832 at fastmail.us wrote:
> 
>> On Tue, Sep 8, 2015, at 12:27, Stephen J. Turnbull wrote:
>> I'm afraid this won't work unless restricted to be the last
>> replacement field, where it just consumes all remaining positional
>> arguments.  I think that restriction deserves a loud "ugh", but maybe
>> it will give somebody a better idea.
> 
> So, this is the second time in as many weeks that I've suggested a new
> !converter, but this seems like the place for it - have something like
> "!join" which "converts" [wraps] the argument in a class whose
> __format__ method knows how to join [and call __format__ on the
> individual members].
> 
> So you could make a list of floating point numbers by "List: {0:,
> |.2f!join}".format([1.2, 3.4, 5.6])
> 
> and it will simply call Joiner([1.2, 3.4, 5.6]).__format__(", |.2f")

I like this version. 

Even without the flexibility, just adding another hardcoded 'j' converter for iterables would be nice, but being able to program it would of course be better.


More information about the Python-ideas mailing list