[Python-ideas] new format spec for iterable types

Paul Moore p.f.moore at gmail.com
Wed Sep 9 16:58:14 CEST 2015

On 9 September 2015 at 15:41, Wolfgang Maier
<wolfgang.maier at biologie.uni-freiburg.de> wrote:
> def unpack_format (iterable, format_spec=None):
>     if format_spec:
>         try:
>             sep, element_fmt = format_spec.split('|', 1)
>         except ValueError:
>             raise TypeError('Invalid format_spec for iterable formatting')
>         return sep.join(format(e, element_fmt) for e in iterable)
>     else:
>         return ' '.join(format(e) for e in iterable)

>From the docs, "The default format_spec is an empty string which
usually gives the same effect as calling str(value)"

So you can just use format_spec='' and avoid the extra conditional logic.

More information about the Python-ideas mailing list