所以我在这里寻找的是类似于PHP的print_r函数。
这样我就可以通过查看相关对象的状态来调试我的脚本。
网友回答:
你真的把两种不同的东西混合在一起。
使用 ,或模块来获取您感兴趣的内容(我以为例;您可以使用任何对象代替)。dir()
vars()
inspect
__builtins__
>>> l = dir(__builtins__)
>>> d = __builtins__.__dict__
随心所欲地打印该词典:
>>> print l
['ArithmeticError', 'AssertionError', 'AttributeError',...
或
>>> from pprint import pprint
>>> pprint(l)
['ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'DeprecationWarning',
...
>>> pprint(d, indent=2)
{ 'ArithmeticError': <type 'exceptions.ArithmeticError'>,
'AssertionError': <type 'exceptions.AssertionError'>,
'AttributeError': <type 'exceptions.AttributeError'>,
...
'_': [ 'ArithmeticError',
'AssertionError',
'AttributeError',
'BaseException',
'DeprecationWarning',
...
漂亮的打印也可以在交互式调试器中作为命令使用:
(Pdb) pp vars()
{'__builtins__': {'ArithmeticError': <type 'exceptions.ArithmeticError'>,
'AssertionError': <type 'exceptions.AssertionError'>,
'AttributeError': <type 'exceptions.AttributeError'>,
'BaseException': <type 'exceptions.BaseException'>,
'BufferError': <type 'exceptions.BufferError'>,
...
'zip': <built-in function zip>},
'__file__': 'pass.py',
'__name__': '__main__'}
网友回答:
您希望与 :vars()
pprint()
from pprint import pprint
pprint(vars(your_object))
网友回答:
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
有许多第三方函数可以添加异常处理、国家/特殊字符打印、递归到嵌套对象等内容。根据作者的喜好。但它们基本上都归结为这一点。
模板简介:该模板名称为【是否有内置函数来打印对象的所有当前属性和值?】,大小是暂无信息,文档格式为.编程语言,推荐使用Sublime/Dreamweaver/HBuilder打开,作品中的图片,文字等数据均可修改,图片请在作品中选中图片替换即可,文字修改直接点击文字修改即可,您也可以新增或修改作品中的内容,该模板来自用户分享,如有侵权行为请联系网站客服处理。欢迎来懒人模板【Python】栏目查找您需要的精美模板。