[Python-Dev] converting the stdlib to str.format

Steven D'Aprano steve at pearwood.info
Thu Jun 5 17:21:22 CEST 2008


On Thu, 5 Jun 2008 10:43:20 pm Nick Coghlan wrote:

> I'm really starting to wonder if supporting positional arguments to
> str.format() *at all* is a mistake. Maybe we should ditch support for
> positional arguments and just accept a single dictionary as the sole
> parameter to format().
>
> For dictionary formatting, str.format() is a clear winner over
> str.__mod__(). For positional formatting I'm not so sure - if someone
> decided to convert from %-formatting to str.format, would it be such
> a burden to ask them to name their substitution variables in the
> process?

If positional arguments are dropped, I would expect to see a 
proliferation of meaningless names used in the dicts:

"items {a} {b}".format(dict(a=spam, b=eggs))

In other words, the names will be just aliases for the position.

I would also expect a performance hit. On my computer, dict(a=spam, 
b=eggs) is two times slower than {'a':spam, 'b':eggs} and nearly three 
times slower than (spam, eggs). I don't imagine building a dict will 
ever be faster than building a tuple. Given that format() is already 
significantly slower than %, why make it slower still?

-1 on dropping positional arguments.


-- 
Steven


More information about the Python-Dev mailing list