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

Russell E. Owen owen at astrono.junkwashington.emu
Mon Jan 7 11:58:48 EST 2002


In article <mailman.1010176028.3427.python-list at python.org>,
 Lewis Bergman <lbergman at abi.tconline.net> wrote:

>Thanks for all the suggestions.
>I made all the changes and cut out about 60 lines of code. What a difference!
>Actually, what is funny is that I started without all the try, except stuff. 
>But, after reading the DB API I added it in. It looks a lot better and I feel 
>better knowing that the errors are raised anyway. Also, the "is" and "is not" 
>tip makes it so much more readable.

I realize this is taken out of context, but this raises a red flag. "is" 
is not a substitute for "=="; they mean very different things. So for 
those who didn't see the original advice (which presumably pointed this 
out), don't try to substitute one for the other.
  "==" means equals, i.e. "has the same value as".
  "is" means "is exactly the same object as".
Most of the time you will want to test for equality ("=="). I'm not sure 
I've ever used "is" in code (except accidentally as an invalid 
substitute for "==" when I started out). (There are times when "is" will 
have the effect of testing for equality, but it's dangerous since you 
are not saying what you mean.)

Regards,

-- Russell



More information about the Python-list mailing list