[Python-Dev] Hooking into super() attribute resolution

Ronald Oussoren ronaldoussoren at mac.com
Tue Jul 9 10:25:41 CEST 2013


On 9 Jul, 2013, at 1:21, Steve Dower <Steve.Dower at microsoft.com> wrote:
>> 
> 
> Except that if it's on a metaclass, the 'instance' it has access to is cls. The descriptor side of things is more interesting, but I see no reason why super can't do that itself, since it knows the actual instance to call __get__ with. (Presumably it already does this with the __dict__ lookup, since that won't call __get__ either.)
> 
> Explaining the new method is easiest if the default implementation is (literally):
> 
> def __getlocalname__(self, name):
>    try:
>        return self.__dict__[name]
>    except KeyError:
>        raise AttributeError(name)
> 
> which does not do any descriptor resolution (and is only a small step from simply replacing __dict__ with a custom object, which is basically where we started). The only change I've really suggested is making it an instance method that can be implemented on a metaclass if you want it for class members.

I like this idea and will experiment with implementing this later this week.  The only thing I'm not sure about is how to indicate that the name could not be found, raising an exception could end up being to expensive if the __getlocalname__ hook gets used in object.__getattribute__ as well. I guess I'll have to run benchmarks to determine if this really is a problem.

Ronald


More information about the Python-Dev mailing list