sufficiently pythonic code for testing type of function

bruno de chez modulix en face bruno at modulix.org
Wed Oct 11 16:12:10 EDT 2006


Theerasak Photha a écrit :

> On 10/11/06, Bruno Desthuilliers <onurb at xiludom.gro> wrote:
>
> > Now the real question : what if the object is not an instance of any of
> > the types, but still support the expected interface ?
>
> Perhaps:
>
> try:
>   for attribute in ['foo', 'bar', '__baz__']:
>     getattr(mystery_object, '__%s__' % attribute)
> except AttributeError:
>   # Do sumthin bout it

Isn't this a case of useless overcomplexification ? Since you end up
raising an exception, why not just assume the object is ok and let
Python raise the exception for you if it is not ? From the client code
POV, it doesn't make much difference !-)


> Is it wrong to 're-raise' an exception with application-specific
> details within an except clause?

Nope - as long as you provide more details (or more helpful details)
*and* do not loose/mask/whatever useful infos from the original
exception.




More information about the Python-list mailing list