[Python-ideas] new format spec for iterable types

random832 at fastmail.us random832 at fastmail.us
Thu Sep 10 01:24:55 CEST 2015



On Wed, Sep 9, 2015, at 18:39, Andrew Barnert via Python-ideas wrote:
> I believe he posted a more detailed version of the idea on one of the
> other spinoff threads from the f-string thread, but I don't have a link.
> But there are lots of possibilities, and if you want to start
> bikeshedding, it doesn't matter that much what his original color was.
> For example, here's a complete proposal:
> 
>     class MyJoiner:
>         def __init__(self, value):
>             self.value = value
>         def __format__(self, spec):
>             return spec.join(map(str, self.value))
>     string.register_converter('join', MyJoiner)

Er, I wanted it to be something more like

def __format__(self, spec):
   sep, fmt = # 'somehow' break up spec into two parts
   return sep.join(map(lambda x: x.__format__(fmt)))

And I wasn't the one who actually proposed user-registered converters;
I'm not sure who did. At one point in the f-string thread I suggested
using a _different_ special !word for stuff like a string that can be
inserted into HTML without quoting. I'm also not 100% sure how good an
idea it is (since it means either using global state or moving
formatting to a class instead of str).

The Joiner class wouldn't have to exist as a builtin, it could be
private to the format function.


More information about the Python-ideas mailing list