什么是PHP的var_dump()的Python等价物? [英] What is a Python equivalent of PHP's var_dump()?

查看:78
本文介绍了什么是PHP的var_dump()的Python等价物?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在PHP中进行调试时,我经常发现在代码中只需粘贴一个 var_dump()就很有用告诉我一个变量是什么,它的价值是什么,对于它包含的任何东西也是一样的。

When debugging in PHP, I frequently find it useful to simply stick a var_dump() in my code to show me what a variable is, what its value is, and the same for anything that it contains.

这是一个很好的Python等价物?

What is a good Python equivalent for this?

推荐答案

要很好地显示一个值,可以使用 pprint 模块。使用它来转储所有变量的最简单的方法是执行

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())

如果您在CGI中运行,一个有用的调试功能是 cgitb 模块,它将局部变量的值显示为部分的追踪。

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.

这篇关于什么是PHP的var_dump()的Python等价物?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆