Why do this?

Matthew Warren Matthew.Warren at Digica.com
Thu Oct 5 08:34:03 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
> 

Habit (not always a good thing..), and it helps keep the distinction as
to what is a number and what is s a string.


> 
> 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__).


Not quite. I was meaning that where I had things naturally in a dict and
found myself concatenating the string, I would now probably use the
substitution method.


> 
> 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

I'm only just really looking into the effects of using things like
__getitem__ etc.., I imagine my approach will become more sophisticated
once I have looked into them.


This email is confidential and may be privileged. If you are not the intended recipient please notify the sender immediately and delete the email from your computer. 

You should not copy the email, use it for any purpose or disclose its contents to any other person.
Please note that any views or opinions presented in this email may be personal to the author and do not necessarily represent the views or opinions of Digica.
It is the responsibility of the recipient to check this email for the presence of viruses. Digica accepts no liability for any damage caused by any virus transmitted by this email.

UK: Phoenix House, Colliers Way, Nottingham, NG8 6AT UK
Reception Tel: + 44 (0) 115 977 1177
Support Centre: 0845 607 7070
Fax: + 44 (0) 115 977 7000
http://www.digica.com

SOUTH AFRICA: Building 3, Parc du Cap, Mispel Road, Bellville, 7535, South Africa
Tel: + 27 (0) 21 957 4900
Fax: + 27 (0) 21 948 3135
http://www.digica.com



More information about the Python-list mailing list