Suppose we are handed a python object. We have no earthly idea where it came from, or what it contains. By calling various functions on the object, how can we coax out a definitive list of names for data the object has references to?
I am only interested in data accessible through python APIs. If an object has some low-level memory buffer which has no corresponding interface in python, then I don't need access to that; but I do want to know how to get access to every python interface an object does have.
My question is similar to asking how can we get a the list of all of a python object's attributes. However, there are some caveats.
I do not want any overloaded __getattribute__
(dot-operator) methods getting in the way. We can bypass any overloaded dot-operators by calling, object.__getattribute__(obj, 'x')
. However, calls to object.__getattribute__
require that we already have access to the names of the attributes we care about. For the usage I envision, we have no idea where the object came from or what the names of its attributes might be. We simply don't know which strings to pass into obj.__getattribute__
python's dir()
method is a step in the right direction, but it is not exactly what we are looking for. dir
will return class methods. However, a python object has no direct references to the class methods. Usually, the object will have a reference to the class which it is instantiated from (through __bases__
). Then, the class is what actually has references to the methods. Class methods are attributes of the class, and not attributes of an instance/object. Thus, dir(obj)
returns things which aren't directly owned by obj
. Another way to view it is that I am seeking single-layer access, not multi-layer access. If a python object (dictionary key) points to another dictionary, which points to another dictionary, and so on, I only want access to the very first dictionary which the name/label refers to. dir
goes and gets the names of things which the object does not have a direct pointer to.
At first I thought that every python object had a 'dict', attribute, and 'dict' was a definitive list of all names the object encapsulated. However, this isn't so. Also, vars(obj)
is unacceptable, since it will throw an error for many python objects, including instances of some of the built-in classes. I am looking for a solution which would tell us a little about ANY python object; not work for some and raise an exception for others.