else clauses in while and for loops

Jonathan Giddy jon at dgs.monash.edu.au
Sun Apr 16 20:28:46 EDT 2000


Chuck Esterbrook declared:
>
>On another topic: With regards to the "try...except" technique above 
>(which I've seen quite a bit in Python) does the following code do the 
>same thing and if so, which do you find more readable?

It does do the same thing with the extra line included below.  Now that
I've looked at this code in more detail than usual, I would probably choose
your form over my original, mainly on the grounds that it's quite likely
for the test to fail, so it's not really "exceptional" behaviour.

However, for code that is executed once over a limited number of
iterations, I'm not losing sleep either way.
>
>_tmpdirs = ['/tmp', '/var/tmp']
>
>for envname in ['TMPDIR', 'TMP', 'TEMP']:
>    if os.environ.has_key(envname):
	 path = [os.environ[envname]]
>        basepaths = validate_paths(path)
>        if basepaths:
>            break
>else:
>    basepaths = validate_paths(_tmpdirs)

Jon.




More information about the Python-list mailing list