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

Eric Smith eric+python-dev at trueblade.com
Thu Aug 16 22:34:19 CEST 2007


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')
The whole point of the indexes is that the order now doesn't matter:
"{0} asks {2} if he is happy to see {1}".format('Brett', 'Christian',
 'Skip')
If you really have many items to expand, name them, and then it matters 
even less:
"{asker} asks {askee} if he is happy to see 
{person}".format(asker='Brett', person='Christian', askee='Skip')
Which I think is way better than the %-formatting equivalent.


More information about the Python-3000 mailing list

AltStyle によって変換されたページ (->オリジナル) /