I would like this to work:
import types
def new_getattr(self, *args, **kwargs):
return 2
class A:
def __init__(self):
pass
a = A()
a.__getattr__ = types.MethodType(new_getattr, a)
print(a.anything)
Right now, it throws AttributeError: A instance has no attribute 'anything'
.
I tried different solutions proposed here and they work, but not for __getattr__
.
If I do print(a.__getattr__('anything'))
, it actually prints 2
; the problem is that my __getattr__
method is not called automatically when I do a.anything
.
As a side note, in my actual implementation, I cannot modify the definition of the class A
, nor can I type its name and do something like A.__getattr__ = ...
(which would work) because I need this to be generic and independent of the class name.
Edit: I ended up doing it like this:
a.__class__.__getattr__ = new_getattr
.