hasattr(obj, attr) overloading?

claudius at catlover.com claudius at catlover.com
Sun Oct 31 10:52:03 EST 1999


gmcm at hypernet.com says:
>claudius writes:
>
>> Is there a way to overload the hasattr method (by default it
>> calls __getattr__ and checks for an exception.  However, my
>> __getattr__ can be a heavy hit that I'd rather avoid for hasattr
>> calls.)
>
>So you are computing attributes?
>
>What would you have hasattr do - return "True, well False, but 
>don't worry about it" <0.5 wink>?
>
>The easiest thing to do is compute the attribute in __getattr__ 
>and assign it to self before returning it. That way subsequent 
>calls won't even go through __getattr__.

I'm storing attributes (and their pickled values) in an RDBMS (MySQL to be
exact) and retrieving and unpickling a longblob just to determine if it exists
is inefficient (and, because I'm storing/retrieving database entries, I can't
rely on the attribute changing out from under me...so caching the data would
require a query [possibly making for two queries for a call to __getattr__.])




More information about the Python-list mailing list