Why do this?
Tim Golden
tim.golden at viacom-outdoor.co.uk
Thu Oct 5 07:55:54 EDT 2006
[Matthew Warren]
| Blame outlook and AutoCaps. If number were a number I would write
|
| print "There are",number,"ways to skin a "+furryanimal
You see now that strikes me as a bit mixed up. Why not simply use?
print "a", number, "c", string
| > altho' print is slated for replacement by a function in Python 3.0
| > at which point they'll be necessary.
|
| ? Why do that, point me at a PEP if required and I'll read it :)
Oft-discussed, and personally I prefer it as a statement,
but the edict has gone out. Look at PEP 3100:
http://www.python.org/dev/peps/pep-3100/
| That's something I wasn't aware of, and I think I'll try if I find
| myself going
|
| "something"+dict['thingummy']+" red ones and "+dict['colour']+" ones"
|
| The dict substitution does feel a bit easier to read compared to the
| concatenation, because of the dict[' '] noise.
Ultimately it's down to you, but I think you may be doing
your code a disservice by so assiduously avoiding the %s-style
of string building. Your last example suggests that you
see the dict-subtitution flavour simply as an alternative to doing
"a" + dict[key1] + "b" + dict[key2] etc. I doubt if I've *ever*
started with the one and ended with the other; rather I've seen that
my code would be more readable if I put/had what I wanted into a
dict (or a dict-like object; just has to support __getitem__).
An easy example of this is where -- like many, I believe -- I prefer my
database rows to come in dict-like objects, rather than the tuples which
the dbapi stipulates. Again, like many, I've used or rolled my own
wrapper
which means I can do things like this, where my dbutils.fetch function
returns some kind of object which traps __getitem__ calls for column
names and returns the appropriate entry in the underlying tuple:
<fake code>
import dbutils
db = db_module.connect (...)
for row in dbutils.fetch (db, "SELECT * FROM blah"):
print "Customer %(name)s (%(id)d) has %(n_orders)d outstanding orders
since %(last_order_date)s" % row
</fake code>
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