Sunday, March 30, 2014

Print the attributes of an object in Python

This can be done easily using,

print object.__dict__

You can also write a function as below.

def dump_an_object(obj):
    for attr in dir(obj):

        print "obj.%s = %s" % (attr, getattr(obj, attr))