
Neil Toronto wrote:
But it seems like you'd need an adapter (see previous reply to Eyal for terminology) for each pair of (self, method). Is there a better way?
I started writing down some ideas for this, but then I realised that it doesn't really extend to attribute lookup in general. The reason is that only some kinds of attribute have their values stored in dict entries -- mainly just instance variables of user-defined class instances. Bound methods, attributes of built-in objects, etc., would be left out. I think the way to approach this is to have a global cache which is essentially a dictionary mapping (obj, name) pairs to some object that knows how to set or get the attribute value as directly as possible. While this wouldn't eliminate dict lookups entirely, in the case of a cache hit it would just be a single lookup instead of potentially many. Some of the ideas behind your adapter might be carried over, such as the idea of callbacks triggered by changes to the underlying objects to help keep the cache up to date. But there would probably have to be a variety of such callback mechanisms for use by different kinds of objects. -- Greg