Is except: ... pass bad style?
Thomas Heller
theller at python.net
Thu Sep 9 13:23:29 EDT 2004
marduk <marduk at python.net> writes:
> I commonly use code like this
>
> try:
> # call optional method
> myobj.method()
> except AttributeError:
> # no biggie
> pass
>
>
> Occasionally I use pylint, which is a good tool, but in the above
> snippet pylint will complain that 'Except doesn't do anything'. True,
> but is that bad style? I know there are other ways of doing it, but
> of all the other "obvious" ones, this appears the most straight
> forward.
>
> Should I ignore pylint or is there a more Pythonic way to do this?
It's better to write it this way, imo:
try:
mth = myobje.method
except AttributeError:
pass
else:
mth()
Otherwise you cannot determine whether myobj doesn't have this method,
or the method call is raising an exception.
Thomas
More information about the Python-list
mailing list