I'm aware that methods are just objects that can be accessed via getattr(obj, 'method_name')
. Is the method does not exist, this will trigger obj.__getattr__(method_name)
. However, is it possible in the __getattr__
implementation to distinct whether the attribute is called directly by the user or not? It seems to me that descriptors might allow this, but I'm not entirely sure.
My motivation is a proxy class that forwards both attribute access and method calls to a wrapped object to which communication is slow. For attribute access, we necessarily have to block and wait for the result. But for method access, I'd like to inject a _blocking
parameter that allows to receive a non-blocking promise object:
proxy = Proxy(Inner())
proxy.value # Block and wait for inner.value
promise = proxy.method(arg1, args2, _blocking=False) # Non-blocking
promise() # Wait for the return value of inner.method(arg1, arg2)