[Tutor] To FORMAT or not to
Steven D'Aprano
steve at pearwood.info
Sun Jan 3 22:46:35 EST 2016
On Sun, Jan 03, 2016 at 01:12:41PM +0000, Alan Gauld wrote:
> Why is it better?
> 1) It is slightly more performant.
Consider that format has to build the entire output as a new string in
advance:
"You've visited {0} & {2}.".format(island, new)
gets generated before being passed to print for printing:
print( result of above )
That involves copying each substring and building a single new string.
Imagine if island and new were each 100MB long texts, you would end up
with a third string, 200MB (plus a bit) in size, before print gets
called. But by calling print directly, it can write each substring (and
separator) to stdout one at a time, without needing to build up the
entire output first.
But even for small strings, it has to parse the template, work out where
the braces are, determine how big the new string will be, allocate that
much memory (possibly causing a garbage collection pause), and copy the
arguments into the new string.
I'm not so sure that all this will be faster than the simple-minded
actions of print:
for each argument:
send str(argument) to stdout;
if this is not the last argument, send the separator
Arguing that format+print is more efficient than print on its own seems
rather unlikely to be correct.
> 2) It improves consistency. You can store the format
> string as a variable and then print it out many times
> from different parts of your code and it will always
> be the same. This makes it much easier to maintain
> your program. For example:
>
> fmtString = "You've visited {0} & {2}."
> if foo:
> print(fmtString.format(foo,bar))
> else:
> print(fmtString.format(baz,bad))
I wouldn't write it like that. I'd write:
if foo:
args = (foo, bar)
else:
args = (baz, bad)
print("You've visited {} & {}.".format(*args))
but now we're out of the realm of simple uses like the Original Poster's
code. The original example gives us no reason to believe that we need to
conditionally change the values being printed, or print it more than
once.
There's nothing wrong with using format. But for something as simple as
this example, you can equally use % or just call print directly. It
isn't an accident that print comes with its own basic formatting
control, it is a deliberate design choice. There's no *need* to
over-complicate the situation and then invent some spurious
justification for it:
"format will save 0.000001 second of time!"
"Maybe some day I'll need to translate this into Klingon!"
"Perhaps I will decide to print the same string from ten different
places in the code!"
Well, sure, but right now, there's no reason to think that one
microsecond will make any noticable difference, or that the program
needs to include translation, or print the string more than once.
I think that, in this specific example, the only really good argument
for using format is a matter of taste: "I prefer to use format, because
I like it. It's more capable, so I use it more often and I am
comfortable and happy using it. There's no strong argument *against*
using it, so I prefer to use it."
Great, that's fair, can't argue with that. There's no need to invent
justifications for format, or to say that format would be better in
*some other situation*. (We don't have some other situation, we have
this situation.) And if you disagree and prefer to just use print,
that's fine too.
--
Steve
More information about the Tutor
mailing list