How to override built-in getattr in Python?

Posted by Stephen Gross on Stack Overflow See other posts from Stack Overflow or by Stephen Gross
Published on 2010-06-16T16:21:52Z Indexed on 2010/06/16 16:42 UTC
Read the original article Hit count: 242

Filed under:

I know how to override an object's getattr() to handle calls to undefined object functions. However, I would like to achieve the same behavior for the builtin getattr() function. For instance, consider code like this:

   call_some_undefined_function()

Normally, that simply produces an error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'call_some_undefined_function' is not defined

I want to override getattr() so that I can intercept the call to "call_some_undefined_function()" and figure out what to do.

Is this possible?

Thanks,

--Steve

© Stack Overflow or respective owner

Related posts about python