[Python-Dev] PEP 447: add type.__locallookup__
Ronald Oussoren
ronaldoussoren at mac.com
Thu Sep 19 10:50:48 CEST 2013
On 13 Sep, 2013, at 14:23, Steven D'Aprano <steve at pearwood.info> wrote:
> On Fri, Sep 13, 2013 at 08:42:46PM +1000, Nick Coghlan wrote:
>> Perhaps "__getdescriptor__" would work as the method name? Yes, it can
>> technically return a non-descriptor,
>
> So technically that name is, um, what's the term... oh yes, "a lie".
>
> :-)
>
>
>> but the *primary* purpose is to
>> customise the retrieval of objects that will be checked to see if they're
>> descriptors.
>
> If that's the case, the PEP should make that clear.
>
> [Aside: the PEP states that the method shouldn't invoke descriptors.
> What's the reason for that? If I take the statement literally, doesn't
> it mean that the method mustn't use any other methods at all? Surely
> that can't be what is intended, but I'm not sure what is intended.]
What it tries to say is that even if cls.__dict__[name] is a descriptor the
__locallookup__ method should not call its __get__ method. That was more relevant
when __locallookup__ had access to the object for which method resolution was performed.
I'm pretty sure this muddies the water even further, but don't have a clearer
description at the moment.
>
>
>> It *won't* be invoked when looking for ordinary attributes in
>> an instance dict, but *will* be invoked when looking on the class object.
>
> Just to be clear, if I have:
>
> instance = MyClass()
> x = instance.name
>
> and "name" is found in instance.__dict__, then this special method will
> not be invoked. But if "name" is not found in the instance dict, then
> "name" will be looked up on the class object MyClass, which may invoke
> this special method. Am I correct?
No. This special method will always be called when "instance.__getattribute__" eventually
calls PyObject_GenericGetAttribute (otherwise all bets are off).
PyObject_GenericGetAttribute always looks for a candicate descriptor in the class (or
one of the other classes on the MRO). When a candidate descriptor is found, and this is
a data descriptor the descriptor is used instead of the value from instance.__dict__ (otherwise
the value from instance.__dict__ is used).
Ronald
More information about the Python-Dev
mailing list