Creating objects you don't actually want

Scott David Daniels Scott.Daniels at Acm.Org
Tue Feb 10 12:12:34 EST 2004


sid wrote:

> ...  this could be written as
> for item in os.lsitdir('dir')
>     try:
>         sfx = sfxobject(item)
>     except sfxobjectError:
>         pass
> 
>>for item in os.listdir('dir'):
>>    if os.isfile(item):
>>        sfx = sfxobject(item)
> this is a better way.

I'd disagree.  Try and handle failure solves funny instances, such as
Just after os.isfile(item) a separate process deletes the file.  Then
sfxobject(item) will fail, because the test tested something that was
true in the past.  Just go ahead and handle failure often works better
than "look before you leap."

-- 
-Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list