Print all properties of a Python Class
PythonOopPython Problem Overview
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?
Python Solutions
Solution 1 - Python
In this simple case you can use [vars()
][1]:
an = Animal()
attrs = vars(an)
# {'kids': 0, 'name': 'Dog', 'color': 'Spotted', 'age': 10, 'legs': 2, 'smell': 'Alot'}
# now dump this in some way or another
print(', '.join("%s: %s" % item for item in attrs.items()))
If you want to store Python objects on the disk you should look at [shelve — Python object persistence][2].
[1]: https://docs.python.org/2/library/functions.html#vars "vars()" [2]: https://docs.python.org/2/library/shelve.html "shelve — Python object persistence"
Solution 2 - Python
Another way is to call the [dir()
][1] function (see [https://docs.python.org/2/library/functions.html#dir][1]).
a = Animal()
dir(a)
>>>
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'age', 'color', 'kids', 'legs', 'name', 'smell']
Note, that [dir()
][1] tries to reach any attribute that is possible to reach.
Then you can access the attributes e.g. by filtering with double underscores:
attributes = [attr for attr in dir(a)
if not attr.startswith('__')]
This is just an example of what is possible to do with [dir()
][1], please check the other answers for proper way of doing this.
[1]: https://docs.python.org/2/library/functions.html#dir "dir"
Solution 3 - Python
Maybe you are looking for something like this?
>>> class MyTest:
def __init__ (self):
self.value = 3
>>> myobj = MyTest()
>>> myobj.__dict__
{'value': 3}
Solution 4 - Python
try ppretty:
from ppretty import ppretty
class Animal(object):
def __init__(self):
self.legs = 2
self.name = 'Dog'
self.color= 'Spotted'
self.smell= 'Alot'
self.age = 10
self.kids = 0
print ppretty(Animal(), seq_length=10)
Output:
__main__.Animal(age = 10, color = 'Spotted', kids = 0, legs = 2, name = 'Dog', smell = 'Alot')
Solution 5 - Python
Here is full code. The result is exactly what you want.
class Animal(object):
def __init__(self):
self.legs = 2
self.name = 'Dog'
self.color= 'Spotted'
self.smell= 'Alot'
self.age = 10
self.kids = 0
if __name__ == '__main__':
animal = Animal()
temp = vars(animal)
for item in temp:
print item , ' : ' , temp[item]
#print item , ' : ', temp[item] ,
Solution 6 - Python
Just try beeprint
it prints something like this:
instance(Animal):
legs: 2,
name: 'Dog',
color: 'Spotted',
smell: 'Alot',
age: 10,
kids: 0,
I think is exactly what you need.