[Python-ideas] %-formatting with Decimals

Andrew Barnert abarnert at yahoo.com
Tue Mar 11 21:10:01 CET 2014


On Mar 11, 2014, at 12:17, random832 at fastmail.us wrote:

> On Tue, Mar 11, 2014, at 9:15, Serhiy Storchaka wrote:
>> 11.03.14 14:44, Oscar Benjamin написав(ла):
>>> Am I doing this wrong? Is this just a limitation of the old-style %
>>> formatting or something that could possibly be improved?
>> 
>> Yes, this is a limitation of the old-style % formatting. Another example:
> 
> Is % formatting not going to be updated to support new features or fix
> bugs like this? 

That's an inherent limitation of % formatting: it uses printf-style type codes, whose meaning is baked into the % operator, rather than calling __format__ on its arguments to handle it polymorphically. %f means treat the argument as a float. That limited flexibility is part of what allows it to be simpler, faster, able to share some format strings with C, etc.

It's doing what it was designed to do; the fact that it wasn't designed the same as str.format isn't a bug.

That's not to say that it couldn't be extended with new type codes for decimals. If C1x adds printf type codes (or if gcc or some other compiler creates its own extension), you could make a pretty good argument for % formatting to borrow them.

> Is it going to be deprecated?

It wasn't deprecated in 3.0, and after the issue was raised again, I'm pretty sure the decision was that deprecation is off the table until at least Python 4000. And if you go back and read the arguments, the reason it wasn't deprecated is that for some use cases it's better, in part because it's not extensible, and has the corresponding advantages mentioned above.

> Will a tool be provided to
> convert usages (at least with static format strings) to str.format()?

I'm pretty sure people have already written such tools, back when str.format was first created. I don't think too many people use them. If %-formatting is working for your existing code, or appropriate for some new code, there's no reason to change it. If {}-formatting is appropriate for some new code, it's easier to write it directly than to try to write a different format and then convert it.



More information about the Python-ideas mailing list