This one seems a bit tricky to me. Sometime ago I already managed to overwrite an instance's method with something like:
def my_method(self, attr): pass instancemethod = type(self.method_to_overwrite) self.method_to_overwrite = instancemethod(my_method, self, self.__class__)
which worked very well for me; but now I'm trying to overwrite an instance's
__getattribute__() function, which doesn't work for me for the reason the method seems to be
Is it possible to do anything about that? I couldn't find any decent Python documentation on
I believe method-wrapper is a wrapper around a method written in C.
There are a couple of methods which you can't overwrite and
__getattribute__() is one of them.
You want to override the attribute lookup algorithm on an per instance basis? Without knowing why you are trying to do this, I would hazard a guess that there is a cleaner less convoluted way of doing what you need to do. If you really need to then as Aaron said, you'll need to install a redirecting
__getattribute__ handler on the class because Python looks up special methods only on the class, ignoring anything defined on the instance.
You also have to be extra careful about not getting into infinite recursion:
class FunkyAttributeLookup(object): def __getattribute__(self, key): try: # Lookup the per instance function via objects attribute lookup # to avoid infinite recursion. getter = object.__getattribute__(self, 'instance_getattribute') return getter(key) except AttributeError: return object.__getattribute__(self, key) f = FunkyAttributeLookup() f.instance_getattribute = lambda attr: attr.upper() print(f.foo) # FOO
Also, if you are overriding methods on your instance, you don't need to instanciate the method object yourself, you can either use the descriptor protocol on functions that generates the methods or just curry the self argument.
#descriptor protocol self.method_to_overwrite = my_method.__get__(self, type(self)) # or curry from functools import partial self.method_to_overwrite = partial(my_method, self)
You can't overwrite special methods at instance level. For new-style classes, implicit invocations of special methods are only guaranteed to work correctly if defined on an object’s type, not in the object’s instance dictionary.