A better way for "except" and "return"?

Sean 'Shaleh' Perry shalehperry at attbi.com
Fri Jan 4 14:46:27 EST 2002


> 
> The whole thing works. I was wondering though. Can some of you with (what 
> seems like decades from watching the list) of experience suggest a couple of 
> ways to make the code tighter or easier to maintain? Any suggestions about 
> other things I have forgotten are welcome as well.
> 

Jason summed up a lot of what I was going to say.  Let me pass on a style tip.

def Function():
    if foo != None:
        ...
        ...
        ...
    else:
        ...
        ...
        ...

can often be better coded as:

def Function():
    if foo == None:
        ...
        return
    ...
    ...
    ...

you get in, make sure you have valid data, then get to work.  Jason did this in
his example function, but was not explicit in the advice.




More information about the Python-list mailing list