[Zope-dev] Re: Bad idiom to access optional attributes

Dieter Maurer dieter at handshake.de
Mon Jun 7 16:54:53 EDT 2004


Casey Duncan wrote at 2004-6-7 09:16 -0400:
>On Mon, 7 Jun 2004 07:46:41 +0200
>Dieter Maurer <dieter at handshake.de> wrote:
>
>[..]
>> Meanwhile, the best way to access optional attributes seems to be
>> 
>>       if getattr(aq_base(obj), attrname, notFound) is not notFound:
>>           attr = getattr(obj, attrname)
>> 	  ...
>
>Ahhh, the miracle of acqfuscation...

The better way would of course be to incapsulate this into
a function, similar to my "hasattr_unacquired"
(which waits as a feature request in Zope's collector
without being touched).
The "hasattr_unacquired" still uses "hasattr"
but "death to 'hasattr'" would need to replace it
with the above "getattr(..., notFound) is not notFound" test.

-- 
Dieter



More information about the Zope-Dev mailing list