If - Or statements

Steven D'Aprano steve at REMOVETHIScyber.com.au
Mon Jun 6 11:43:34 EDT 2005


On Sun, 05 Jun 2005 13:52:09 -0400, Roy Smith wrote:

> One sure sign of somebody trying to write C in Python is when they loop 
> over a list by doing
> 
> for i in range (len (myList)):
>    doSomethingWith (myList[i])

I usually do that, and I've never coded a line of C in my life. I can't
even read C :-)

Of course, the main reason I use that form is that I frequently have to
use older versions of Python without enumerate, and therefore have got
into the practice of doing without it.

Of course, if I'm just reading the items of myList, I don't bother with
the index, and just use 

for item in myList:
    dosomethingwith(item)

> or when they laboriously check for every possible error condition before 
> performing an operation instead of just doing it and catching the exception.

This is not always a bad idea. For example, if you are modifying an
object in place, and an error halfway through could leave the object in an
inconsistent state, it may be better to check for error conditions first
rather than try to back out of it after you've run into trouble.

But still, in general I agree with your observation. 


-- 
Steven





More information about the Python-list mailing list