I have a class Animal with several properties like:


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...

I now want to print all these properties to a text file. The ugly way I’m doing it now is like:


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

Is there a better Pythonic way to do this?

6 Answers
6

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *