我有一个Animal类,它有几个属性,比如:
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...
现在,我想将所有这些属性打印到一个文本文件中。我现在使用的丑陋方式是这样的:
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,)
有没有更好的Pythonic方法来做这件事?
https://stackoverflow.com/questions/5969806
复制相似问题