Last active
August 29, 2015 14:02
-
-
Save morozgrafix/25255c2236b3b7927e11 to your computer and use it in GitHub Desktop.
var_dump in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def var_dump(obj): | |
'''return a printable representation of an object for debugging''' | |
newobj=obj | |
if '__dict__' in dir(obj): | |
newobj=obj.__dict__ | |
if ' object at ' in str(obj) and not newobj.has_key('__type__'): | |
newobj['__type__']=str(obj) | |
for attr in newobj: | |
newobj[attr]=var_dump(newobj[attr]) | |
return newobj | |
class stdClass(object): pass | |
obj=stdClass() | |
obj.int=1 | |
obj.tup=(1,2,3,4) | |
obj.dict={'a':1,'b':2, 'c':3, 'more':{'z':26,'y':25}} | |
obj.list=[1,2,3,'a','b','c',[1,2,3,4]] | |
obj.subObj=stdClass() | |
obj.subObj.value='foobar' | |
from pprint import pprint | |
pprint(var_dump(obj)) | |
#{'__type__': '<__main__.stdClass object at 0x2b126000b890>', | |
# 'dict': {'a': 1, 'c': 3, 'b': 2, 'more': {'y': 25, 'z': 26}}, | |
# 'int': 1, | |
# 'list': [1, 2, 3, 'a', 'b', 'c', [1, 2, 3, 4]], | |
# 'subObj': {'__type__': '<__main__.stdClass object at 0x2b126000b8d0>', | |
# 'value': 'foobar'}, | |
# 'tup': (1, 2, 3, 4)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment