1

Suppose we want to get an object's attribute: smth = object.attr. Also suppose we already know what object our attribute is located in, let it be the class called A.

As far as I know, the attribute getting procedure then looks like this:

attr = A.__dict__['attr']      # 1
if hasattr(attr, '__get__'):   # 2
    return attr.__get__(None, A)
else: 
    return attr
  1. Find the descriptor in the attribute dictionary of class A.
  2. Return an output of the attribute's getter if it's there. Otherwise, return the attribute itself.

I see a problem here. Suppose our attribute is a descriptor with a getter. In that case, to resolve object.descriptor, we have to resolve descriptor.__get__ which, in turn, is a non-data descriptor (function) and needs to be resolved too. Where is the end of this recursion?

Alexander Lutsenko
  • 2,130
  • 8
  • 14
  • Resolving `attr.__get__` invokes `attr.__getattr[ibute]__`, not `attr.__get__` itself, so there's no recursive case. Or have I misunderstood the question? Which method(s) do you think lead(s) to recursion? – jonrsharpe Nov 06 '15 at 13:42
  • @jonrsharpe `attr.__get__` is an ordinary function, right? So, before using it we must: 1) find it; 2) bind it, i.e. invoke `(attr.__get__).__get__`. But the latter is an ordinary function, so... – Alexander Lutsenko Nov 06 '15 at 13:58
  • 1
    `attr.__get__` is not itself a descriptor, though, so doesn't have a `.__get__` to find. It's not clear to me why you think this is recursive. – jonrsharpe Nov 06 '15 at 13:59
  • @jonrsharpe Thanks. Indeed, it turns out that function.__get__ is not a function but a method-wrapper (as is properpty.__get/set/delete__). And yes, method-wrapper is not a descriptor. – Alexander Lutsenko Nov 06 '15 at 14:19
  • 1
    If you're new to descriptors, have a look at e.g. https://docs.python.org/2/howto/descriptor.html – jonrsharpe Nov 06 '15 at 14:20
  • @jonrsharpe I just had. Oddly, it has no mention of the fact that built-in 'magic' methods (`__get__`, `__set__`, `__delete__`) of built-in descriptors are not stored as functions, as opposed to normal methods. Or maybe every built-in method is a method-wrapper? – Alexander Lutsenko Nov 06 '15 at 14:41

0 Answers0