Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #75306

Re: What meaning of this ""hello %s you are %s years old" % x"

References <cefbd1ff-b65f-43ec-947e-2324085a6486@googlegroups.com> <53D55ACA.5090901@islandtraining.com> <1406533295.63026.YahooMailNeo@web163804.mail.gq1.yahoo.com>
Date 2014-07-28 17:58 +1000
Subject Re: What meaning of this ""hello %s you are %s years old" % x"
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.12384.1406534294.18130.python-list@python.org> (permalink)

Show all headers | View raw


On Mon, Jul 28, 2014 at 5:41 PM, Albert-Jan Roskam
<fomcl@yahoo.com.dmarc.invalid> wrote:
>> That's not tuple%tuple, but rather string%tuple.  And string%tuple is
>> the older method of formatting an output string from a template and a
>> tuple of values.  See
>> https://docs.python.org/2/library/stdtypes.html#string-formatting for
>> details.
>>
>> However, if you are just learning Python, you should probably use the
>> *newer* formatting operations.  See
>> https://docs.python.org/3.3/library/string.html#formatspec for details
>> of that.
>
> Do you know what was the reason/consideration to switch to a new formatting operation? Ability to have custom formatters with an own __format__ method?

Flexibility. You can do a few things with the other formatting style
that you can't do with percent-formatting. However, percent formatting
isn't going anywhere, and there's no particular reason to avoid it,
even in brand new code. It's more portable across languages (heaps of
C-inspired languages have a printf-style function that responds to the
same notations), more compact, and ample to a lot of situations, so
there's no need to go for the other style.

The only real downside of percent formatting is that, since it's an
operator rather than a function call, it can take only one argument -
so there's some magic with tuples, and a few extremely obscure corner
cases as a result. Not a reason to avoid it.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

What meaning of this ""hello %s you are %s years old" % x" fl <rxjwg98@gmail.com> - 2014-07-27 11:49 -0700
  Re: What meaning of this ""hello %s you are %s years old" % x" Gary Herron <gary.herron@islandtraining.com> - 2014-07-27 13:02 -0700
  Re: What meaning of this ""hello %s you are %s years old" % x" Dan Stromberg <drsalists@gmail.com> - 2014-07-27 14:37 -0700
  Re: What meaning of this ""hello %s you are %s years old" % x" Albert-Jan Roskam <fomcl@yahoo.com> - 2014-07-28 00:41 -0700
  Re: What meaning of this ""hello %s you are %s years old" % x" Chris Angelico <rosuav@gmail.com> - 2014-07-28 17:58 +1000

csiph-web