On Wed, Jun 7, 2017 at 2:44 AM, Peter Pearson <pkpearson@nowhere.invalid> wrote: > On Tue, 6 Jun 2017 03:10:05 +1000, Chris Angelico wrote: >> On Tue, Jun 6, 2017 at 3:01 AM, Peter Pearson wrote: > [snip] >>> Say >>> "foo %s" % (1-2) >>> not >>> ("foo %s" % 1) - 2 >>> . >>> >>> Personally I prefer a less compact but more explicit alternative: >>> >>> "foo {}".format(1-2) >> >> The trouble with the zen of Python is that people now use the word >> "explicit" to mean "code that I like". What is more explicit here? It >> uses a method call instead of an operator, but either way, it's >> completely explicit that you want to populate the placeholders in a >> template string. > > I meant simply that when you call "format", you don't have to think > about the precedence of the "%" operator. Maybe "explicit" isn't > the right word; I just make fewer mistakes when I stick with things > that I use all the time (function calls, in this case).
Thank you, that's a more accurate way to describe it. If you don't want to memorize precedence but like the simplicity of percent formatting, try this: def fmt(str, args): return str % args fmt("foo %s", 1-2) Also avoids the problem of formatting a tuple the wrong way by mistake. ChrisA -- https://mail.python.org/mailman/listinfo/python-list