[Python-Dev] converting the stdlib to str.format
Steven D'Aprano
steve at pearwood.info
Thu Jun 5 16:05:57 CEST 2008
On Thu, 5 Jun 2008 08:50:32 pm Paul Moore wrote:
> On 05/06/2008, Steven D'Aprano <steve at pearwood.info> wrote:
> > Yes, I don't particularly see the advantage of writing:
> >
> > "spam %s spam" % (value,)
> >
> > over
> >
> > "spam %s spam" % value
> >
> > Why require the first version?
>
> Because the second breaks if value is a tuple:
That's a good reason for not using the second form if value is an
arbitrary object that could be any type at all. But how often does that
happen in real code? It's rare that the right hand argument can be any
arbitrary type.
I've looked at my code, and there's not one occasion that I've needed to
write "foo %s" % (obj,) to guard against obj unexpectedly being a
tuple. If obj was a tuple, it would fail long before it reached the
string expression. Of course, your mileage may vary.
As I see it, your argument is one for consistency: since *sometimes* you
need to wrap the right-hand argument in a tuple, then we should insist
in *always* wrapping it in a tuple. But of course you can do that right
now:
>>> n = 1
>>> "number %d" % (n,) # just in case n is a tuple
'number 1'
Just don't force me to do it.
--
Steven
More information about the Python-Dev
mailing list