[Python-3000] Please don't kill the % operator...

Brett Cannon brett at python.org
Thu Aug 16 22:19:49 CEST 2007


On 8/16/07, skip at pobox.com <skip at pobox.com> wrote:
>
>     Brett> But how is::
>
>     Brett>   "{0} is happy to see {1}".format('Brett', 'Christian')
>
>     Brett> that less easier to read than::
>
>     Brett>   "%s is happy to see %s" % ('Brett', 'Christian')
>
>     Brett> ?  Yes, PEP 3101 style is more to type but it isn't grievous; we
>     Brett> have just been spoiled by the overloading of the % operator.  And
>     Brett> I don't know how newbies think these days, but I know I find the
>     Brett> numeric markers much easier to follow then the '%s', especially
>     Brett> if the string ends up becoming long.
>
> If you decide to insert another format token in the middle the new is more
> error-prone than the old:
>
>     "{0} asks {1} if he is happy to see {1}".format('Brett', 'Skip', 'Christian')
>
>                                         ^^^ whoops
>
> vs:
>
>     "%s asks %s if he is happy to see %s" % ('Brett', 'Skip', 'Christian')
>
> Now extend that to format strings with more than a couple expansions.
>

Sure, but I find the %s form harder to read honestly.  Plus you didn't
need to insert in that order; you could have put it as::

  "{0} asks {2} if he is happy to see {1}".format("Brett", "Christian", "Skip")

Not perfect, but it works.  Or you just name your arguments.  We are
talking simple things here, and as soon as you start to scale you will
most likely move to name-based arguments anyway once your quick hack
format scheme doesn't hold.

>     Brett> This is where the cranky python-dev'er comes in: PEP 3101 was
>     Brett> published in April 2006 which is over a year ago!  This is not a
>     Brett> new PEP or a new plan.
>
> Yes, but Python 3 is more real today than 15 months ago, hence the greater
> focus now than before.

Well, for me the "realness" of Py3K is the same now as it was back
when Guido created teh p3yk branch.

-Brett


More information about the Python-3000 mailing list