har7old
har7old

Reputation: 1

Understanding __dict__ search during the attribute lookup

I recently learned that the __dict__ "attribute" of an instance in python is implemented as a descriptor object in that instance's class. This raised a question for me: does python really make such a long chain of calls to find the __dict__ of an instance when it is needed in the attribute lookup process?

class Test:
    pass

t = Test()
t.a = 5

type(type(t)).__dict__['__dict__'].__get__(Test)['__dict__'].__get__(t)['a'] # returns 5

I know that when searching for __dict__ to get other attributes, __getattribute__ is not called individually for it, but does python search for descriptor objects, or does __dict__ really exist as some secret instance attribute at the python implementation level (e.g. cpython), and is found immediately?

Upvotes: 0

Views: 81

Answers (0)

Related Questions