variable declaration

Michael Tobis mt at 3planes.com
Tue Feb 1 11:29:24 EST 2005


> How common is it for a local variable to be bound in
> more than one place within a function?

It's more natural for a beginner to read or write

.mystr = ""
.for snippet in snippets:
.   if ilike(snippet):
.      mystr = mystr + snippet

than

.mylist = []
.for snippet in snippets:
.   if ilike(snippet):
.      mylist.append(snippet)
.mystr = "".join(mylist)

for instance.

While the latter is superior in some ways, I frequently find my fingers
tossing off the former approach.

Of course in this case it's not hard to come up with

mystr = "".join([snippet in snippets if ilike(snippet)])

but it's also not too hard to imagine cases where the list
comprehension would be too complex or would require too much
refactoring.

I don't know that it's ever necessary to rebind, but it is, in fact,
common, and perhaps too easy. In numeric Python, avoiding rebinding
turns out to be a nontrivial skill. 

mt




More information about the Python-list mailing list