Search code examples
pythonintrospection

Is it possible to introspect actual attributes/methods of an object which overrides `__dir__` and `__getattribute__`?


Some answers from this question bring very silly ways to cripple the ability to access methods and attributes for instances of objects overriding __dir__ and __getattribute__.

The attributes and methods are still there, but are they really inaccessible?

For example, type(x) still returns the correct answer even if x.__class__ raises AttributeError.

Is there any way to access the hidden methods and attributes?


Solution

  • For instances of a new-style class you could do something like this:

    object.__getattribute__(instance, '__dict__')
    

    I got the idea while reading a section titled More attribute access for new-style classes in the documentation, where they suggest doing something like that to avoid infinite recursion in its implementation.