[Tutor] Pattern to handle exceptions

Negroup - negroup at gmail.com
Sat Sep 24 02:17:58 CEST 2005


Hi all, I have a question about the following lines of code:

>>> class Foo:
...     bar = 'bar'
...
>>> # is it better this
>>> def te(attribute):
...     try:
...             print getattr(c, attribute)
...     except AttributeError:
...             return None
>>> #
>>> # or this?
>>> def h_a(attribute):
...     if hasattr(c, attribute):
...             return getattr(c, attribute)
...     else:
...             return None
...

Generally, to handle potentially erroneus situations, which pattern is
most correct to use?

TIA


More information about the Tutor mailing list