Help please: How to assign an object name at runtime

Steven D'Aprano steve at REMOVEMEcyber.com.au
Thu Jun 30 00:08:26 EDT 2005


John Machin wrote:

> BTW, don't use "l".

Excellent advice.

But since the original poster appears to be rather a 
newbie, perhaps a little bit of explanation would be 
useful.

Variables like l and I should be avoided like the 
plague, because in many fonts and typefaces they are 
indistinguishable, or look like the digit 1. Yes, I'm 
sure you are using a fancy syntax-highlighting editor 
that colours them differently, but the day will come 
that you are reading the code on pieces of dead tree 
and then you'll be sorry that you can't tell the 
difference between l and 1.

Another bit of advice: never use words like "list", 
"str", "int" etc as names for variables, because they 
conflict with Python functions:

py> list("AB")
['A', 'B']
py> list = []
py> list("AB")
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: object of type 'list' is not callable

In general, I only use single letter variable names 
like a, b, L (for a list), s (for a string) in short 
function code, where the nature of the variable is 
obvious from the context:

def stepsum(n, step=1):
     total = 0
     for i in range(0, n, step):
         total + i
     return total

def add_colon_str(s, t):
     return s + ": " + t

It doesn't need much explanation to understand what s 
and t are. But for anything more complex, I always use 
descriptive names.


-- 
Steven.




More information about the Python-list mailing list