Abuse of the object-nature of functions?

Peter Otten __peter__ at web.de
Tue Jul 11 11:08:59 EDT 2006


Fredrik Lundh wrote:

> Sybren Stuvel wrote:
> 
>> Ant enlightened us with:
>>>     try:
>>>         assertion = callable.is_assertion
>>>     except:
>>>         pass
>>
>> Try to make a habit out of catching only the exceptions you know will
>> be thrown. Catching everything generally is a bad idea. In this case,
>> my bet is that catching AttributeError is enough.
> 
> and
> 
>     assertion = hasattr(callable, "is_assertion")
> 
> is even nicer (and can be done as part of the if-statement after the call,
> in- stead of in a separate try/except before the call)

You would normally expect that you can turn off a flag by setting it to
False instead of deleting it -- which is also how the OP's code works. So I
would prefer

assertion = getattr(callable, "is_assertion", False)

if you forgo the explicit try...except.

Peter



More information about the Python-list mailing list