Is except: ... pass bad style?

Istvan Albert ialbert at mailblocks.com
Thu Sep 9 21:29:08 CEST 2004


marduk wrote:

> 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 personally don't like this, not because of pylint but because
it feels like using the wrong solution for the problem.
You could create a base class that has a method that
does nothing and have all your objects inherit from that.

This solution does properly communicate
that the method is optional. Not to mention that you need
to repeat the whole thing if used again in a different
circumstance. Plus something could go wrong inside
existing methods and you'd never know about it.

Why risk all that?

Istvan.



More information about the Python-list mailing list