Python wart

Chris Angelico rosuav at
Fri Nov 1 02:53:04 CET 2013

On Fri, Nov 1, 2013 at 12:45 PM, Mark Lawrence <breamoreboy at> wrote:
> Quite often I type this
> print('Total of accounts %.2f', total)
> when I meant to type this
> print('Total of accounts %.2f' % total)
> Do I have to raise a PEP to get this stupid language changed so that it
> dynamically recognises what I want it to do and acts accordingly?

I feel your frustration there, especially since the usage you're
attempting parallels C's printf function. But the parsing would be
ambiguous - is it that any percent sign changes the meaning of print's
arguments? Or is this some kind of DWIM system, where the meaning
depends on whether the arguments "would work" if you used a percent

What you _could_ do, though, is decide that you will never EVER use
print with multiple arguments. Then just do this:

orig_print = print
def print(fmt, *args, **kwargs):
    orig_print(fmt%args, **kwargs)

Now your print function takes printf-style arguments. Note that this
WILL error out if you have percent signs and no arguments; you may
want to use "if args" to guard against that, or else just accept that
it'll break if you do stuff that would be broken with printf.

(You may want to play with functools.wraps for the benefit of
docstrings and stuff... or maybe you don't care.)

One of the advantages of print being a function is that you can
actually do this :) With print as a statement, that option isn't


More information about the Python-list mailing list