Confused with python attribute getting process: infinite recursion?

152 views Asked by At

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?

0

There are 0 answers