[Python-ideas] PEP 447: Adding type.__getdescriptor__
Nick Coghlan
ncoghlan at gmail.com
Fri Dec 1 05:32:11 EST 2017
On 1 December 2017 at 19:15, Ronald Oussoren <ronaldoussoren at mac.com> wrote:
> Maybe, but how would this work with super()? Super walks the MRO of type of
> the instance, but skips the class on the MRO. This is not equivalent to
> walking the MRO of the second class on the MRO when you use multiple
> inheritance,
>
> This also has some other disadvantages. The first is that
> tp.__getdescriptor__ would replace the default behaviour for the entire MRO
> and it would be possible to have different behavior for classes on the MRO.
> The second, minor. one is that __getdescriptor__ would have to reimplement
> the default logic of walking the MRO, but that logic is fairly trivial.
I believe those can both be addressed by structuring the override a
little differently, and putting it at the level of individual
attribute retrieval on a specific class (rather than on all of its
subclasses):
def _PyType_Lookup(tp, name):
mro = tp.mro()
assert isinstance(mro, tuple)
for base in mro:
assert isinstance(base, type)
try:
getdesc = base.__dict__["__getdescriptor__"]
except KeyError:
try:
return base.__dict__[name]
except KeyError:
pass
else:
try:
return getdesc(tp, base, name)
except AttributeError:
pass
return None
In that version, the __getdescriptor__ signature would be:
def __getdescriptor__(dynamic_cls, base_cls, attr):
...
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
More information about the Python-ideas
mailing list