Why do this?

Tim Golden tim.golden at viacom-outdoor.co.uk
Thu Oct 5 06:41:18 EDT 2006


[Matthew Warren]

| Now, I started programming when I was 8 with BBC Basic.

Hey, likewise! (Except I was 12 when it came out!)

| I learned over the years to do things like the following, and I like
| doing it like this because of readability, something Python seems to
| focus on :-
| 
| Print "There are "+number+" ways to skin a "+furryanimal

perfectly sound Python code, as far as it goes, altho' obviously
"Print" is spelt "print" in Python, and if that number is in fact
a number it'll need to be str ()-ed first.

| But nowadays, I see things like this all over the place;
| 
| print("There are %s ways to skin a %s" % (number, furryanimal))

The outermost brackets are (at the moment) unnecessary in python,
altho' print is slated for replacement by a function in Python 3.0
at which point they'll be necessary.

number = 3
animal = "cat"
print "There are %d ways to skin a %s" % (number, animal)

| Now I understand there can be additional formatting benefits when
| dealing with numbers, decimal places etc.. But to me, for strings, the
| second case is much harder to read than the first.

I think what it comes down to is just what's most readable in the
situation in which you're using it. Imagine a rather longer
string, and one where <number> and <animal> are not short names,
but calls to some function. Sometimes, since %s will call __str__
on anything it's passed, it's a quick way to get a string representation
of a more complex object, which you'd otherwise have to str () at
least.

| I hope I'm not being dense.

Nothing dense about it. Very often a matter of taste and style.
Sometimes 
I find myself using string substitution where a simple string addition 
might suffice simply because of consistency, for example where it's 
surrounded by some more complex cases as described above, and I don't 
want to break the pattern simply because the case is simpler.

Also, for me, substitution often reads more naturally because you're 
not breaking the string up with lots of " + xxxx + "..." + yyy + "...  
stuff. Again, though, depends on what your code looks like, and how 
readable you find it. Certainly, I wouldn't advocate *pathologically* 
avoiding the "%s" % blah style, but it needn't always be the right
thing.

A typical example where it can become cumbersome is when you're
partially
reinventing a templating system with quite a long string, say of HTML,
and
quite a few substitutions right at the end. It may be easier to use the 
dictionary form of the substitution then, eg:

print """
Long string with %(valueA)s and %(valueB)s and %(valueC)s embedded in
it at some distance from the end...
...
""" % dict (valueA=1, valueB="blah", valueC=datetime.datetime.now ())

Obviously that dict would likely be defined elsewhere, or could even
be locals (). Once you get to this stage, though, you might want to 
start looking at templating toolkits like Cheetah, Genshi or any of
the many others knocking around.

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list