How best to write this try/except block?

Fredrik Lundh fredrik at pythonware.com
Wed Apr 3 16:35:37 EST 2002


Rich Harkins wrote:

> Because of the is comparison even if "MYBLANKVALUE" is in d then this code
> will still work.  Just don't intern whatever "MYBLANKVALUE" is (I frequently
> use undefined="UNDEFINED").

bad idea.  Python automatically interns string literals that happen
to look like identifiers (see the "Intern selected string constants"
section in Python/compile.c), so this only works if you can guarantee
that your "fn" function is never called with a string literal.

undefined=["UNDEFINED"] is a bit safer, but you really should use
has_key or try/except for things like this.

</F>





More information about the Python-list mailing list