Analyzing Objects in Python

There’s a built-in Python function called vars that can be useful in analyzing a Python object.

Just as running

dir(the_object)

returns a listing of all of the attributes of an object, including it’s methods, the following code, will return key/value pairs for the named attributes (variables), but NOT the methods (functions):

for k, v in vars(the_object).items():
               print k,v
import sys
sys.getsizeof(object) # or
sys.getsizeof(object.attribute) 

Can also be useful, however the complete amount of memory is often hidden behind references.

Lastly (for now) there’s a relatively new Python package called dill that is a wrapper (interface?) for Python’s pickle package, and dill has a class called detect that returns a whole bunch of details about python objects:

dill.detect.PY3          dill.detect.children     dill.detect.iscode       dill.detect.nested       dill.detect.reference
dill.detect.at           dill.detect.code         dill.detect.isframe      dill.detect.nestedcode   dill.detect.trace
dill.detect.baditems     dill.detect.errors       dill.detect.isfunction   dill.detect.outermost    dill.detect.varnames
dill.detect.badobjects   dill.detect.freevars     dill.detect.ismethod     dill.detect.parent
dill.detect.badtypes     dill.detect.globalvars   dill.detect.istraceback  dill.detect.parents