hasattr(obj, attr) overloading?

Gordon McMillan gmcm at hypernet.com
Sun Oct 31 07:54:37 EST 1999


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__.
 


- Gordon




More information about the Python-list mailing list