[Python-3000] Implementing % formatting in terms of str.format()

Nick Coghlan ncoghlan at gmail.com
Sat Apr 12 15:49:43 CEST 2008


skip at pobox.com wrote:
>     >> Is there a 2-to-3 fixer for % format?  I scanned the fixes directly
>     >> quickly but didn't see anything obvious.
> 
>     Benjamin> I believe the only reason that % is even in 3.0 is that a 2to3
>     Benjamin> fixer couldn't be easily written for it.
> 
> I find that kind of hard to believe (that it should be terribly difficult to
> write a fixer, at least given a % operator with a string literal LHS and
> either a tuple or dict RHS or a call to locals() or globals()).

That's exactly the problem though - while a 2to3 fixer can be written 
for a tiny subset of formatting calls (those that meet the constraints 
you gave), the vast majority are out of luck without some major type 
inferencing additions to 2to3. Given the expression "x % y", 2to3 hasn't 
got a clue whether it needs to do anything unless it somehow knows the 
types of x and y.

So my understanding matches Benjamin's: while string %-formatting is 
definitely a 'second way' to do something for which str.format will be 
the preferred approach, getting rid of it for Py3k just isn't worth the 
staggering amount of breakage that would result.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-3000 mailing list