[Python-Dev] PEP 447: add type.__locallookup__

Steven D'Aprano steve at pearwood.info
Fri Sep 13 05:59:17 CEST 2013


On Fri, Sep 13, 2013 at 03:04:49AM +0000, Steve Dower wrote:

> What about __getlocalattribute__ or __getattributenorecurse__? Long, 
> but this isn't going to be used often.

This has nothing to do with locals, nor does it have anything to do with 
recursion, so both those names are misleading.


> Putting "type" or "class" in the name would be misleading. It's an 
> instance method (that is most useful when implemented on a metaclass).

Regardless of whether it is an instance method or not, by default it 
performs the lookup on the type. Hence the C function _PyType_Lookup and 
hence my suggestion __typelookup__.

But I think that __typelookup__ does describe quite well what the method 
does. It looks up on the type. The PEP is fairly clear on how this is 
supposed to work, e.g. the default type.__<whatever>__ method will look 
up in the class/type dict. PEP 447 includes an example of how you might 
implement this in Python:

class MetaType(type):
    def __locallookup__(cls, name):
        try:
            return cls.__dict__[name]
        except KeyError:
            raise AttributeError(name) from None


"local lookup" doesn't even come close to describing what the method 
does or why you would use it. It suggests something to do with locals, 
which is not the case. Neither does __getattributenorecurse__, which 
suggests looking up an attribute on an object without following the 
inheritance hierarchy, e.g. looking in the instance __dict__ but not the 
class __dict__. So the complete opposite of what it actually does.


-- 
Steven


More information about the Python-Dev mailing list