所以我在这里寻找的是像PHP的print_r函数.这样我就可以通过查看相关对象的状态来调试我的脚本.
python debugging introspection pretty-print python-datamodel
我有一个类动物,有几个属性,如:
class Animal(object):
def __init__(self):
self.legs = 2
self.name = 'Dog'
self.color= 'Spotted'
self.smell= 'Alot'
self.age = 10
self.kids = 0
#many more...
Run Code Online (Sandbox Code Playgroud)
我现在想要将所有这些属性打印到文本文件中.我现在这样做的丑陋方式就像:
animal=Animal()
output = 'legs:%d, name:%s, color:%s, smell:%s, age:%d, kids:%d' % (animal.legs, animal.name, animal.color, animal.smell, animal.age, animal.kids,)
Run Code Online (Sandbox Code Playgroud)
有更好的Pythonic方法吗?