These two patterns produce the same results. Does it matter which one is used? Why?
I prefer the second, it has less indentation and just looks cleaner to me, but I haven't seen it used much (in the places I've been). I don't want to settle on something and use it all over if it's ill advised for some reason.
IF...ELSE
if not packages:
help('download')
else:
for p in packages:
do_download(p)
verify_md5(p)
etc(p)
IF...RETURN; IMPLICIT ELSE
if not packages:
help('download')
return
for p in packages:
do_download(p)
verify_md5(p)
etc(p)
From The Zen of Python:
So the second approach is more Pythonic.
I personally find flat code easier to read than nested code, and less error-prone as well (having e.g. an
else
statement not properly lined up can be hard to debug). Of course, these are subjective judgments.