if / try

vald vald at valis.amber.eu.org
Thu Oct 2 09:29:50 EDT 2003


I wonder if there is any difference in using
if/else and try/except when checking some
return values in functions. For example:

    def getValue(self):
        try:
            return self.values["SOME_VALUE"]
        except KeyError:
            return None

    def getValue2(self):
        if self.values.has_key("SOME_VALUE"):
            return self.values["SOME_VALUE"]
        else:
            return None

Is there any preformance gain when using one of
those methods? Which one should be widely used
in applications in such kind of methods that just
check whether something is valid and return value 
or None?

Regards,
 Kamil
      





More information about the Python-list mailing list