[Zope-dev] zope.interface: verifyObject vs properties

Philipp von Weitershausen philipp at weitershausen.de
Wed Oct 15 12:16:11 EDT 2008


Thomas Lotze wrote:
> Jim Fulton <jim at zope.com> wrote:
> 
>> I would change it to just use getattr rather than hasattr.
>>
>> try:
>>     getattr(ob, name)
>> except AttributeError:
>>     return False
>> ...
> 
> This doesn't handle the case that the attribute exists as a property
> but raises an AttributeError when trying to produce its value.

Yes, but this is still better than hasattr() which eats all exceptions. 
I think eating AttributeErrors is fine because to the user of 'ob', it 
would seem that ob.attr wouldn't exist anyway if it threw an 
AttributeError (even if that exception was about something else, it's 
still an AttributeError).

I suggest doing

   marker = object()
   return getattr(ob, name, marker) is marker

rather than

   return hasattr(ob, name)


More information about the Zope-Dev mailing list