What is a Python equivalent of PHP's var_dump()?
--
Track title: CC D Schuberts Piano Sonata D 850 in D
--
Chapters
00:00 Question
00:30 Accepted answer (Score 325)
00:58 Answer 2 (Score 348)
01:16 Answer 3 (Score 70)
01:34 Answer 4 (Score 31)
02:16 Thank you
--
Full question
https://stackoverflow.com/questions/3839...
Question links:
[var_dump()]: http://php.net/var-dump
Accepted answer links:
[pprint]: http://docs.python.org/library/pprint.ht...
[cgitb]: http://docs.python.org/library/cgitb.htm...
Answer 2 links:
[print]: https://docs.python.org/library/function...
[vars]: https://docs.python.org/library/function...
Answer 3 links:
[PHP]: http://en.wikipedia.org/wiki/PHP
Answer 4 links:
https://github.com/sha256/python-var-dum...
[pip]: http://pip.openplans.org/
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#php #python #debugging
#avk47
ANSWER 1
Score 350
I think the best equivalent to PHP's var_dump($foo, $bar) is combine print with vars:
print vars(foo),vars(bar)
ACCEPTED ANSWER
Score 332
To display a value nicely, you can use the pprint module. The easiest way to dump all variables with it is to do
from pprint import pprint
pprint(globals())
pprint(locals())
If you are running in CGI, a useful debugging feature is the cgitb module, which displays the value of local variables as part of the traceback.
ANSWER 3
Score 76
The closest thing to PHP's var_dump() is pprint() with the getmembers() function in the built-in inspect module:
from inspect import getmembers
from pprint import pprint
pprint(getmembers(yourObj))
ANSWER 4
Score 30
PHP's var_export() usually shows a serialized version of the object that can be exec()'d to re-create the object. The closest thing to that in Python is repr()
"For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval() [...]"