Is there any method to let me know the values of an object’s attributes?
For example, info = urllib2.urlopen('http://www.python.org/')
I wanna know all the attributes’ values of info. Maybe I don’t know what are the attributes the info has. And str() or list() can not give me the answer.
Is there any method to let me know the values of an object’s attributes?
Share
To get all the names of object’s attributes, use
dir(obj). To get their values, usegetattr(obj, attr_name). You could print all the attributes and their values like so:If you don’t need the built-in attributes, such as
__str__etc, you can simply useobj.__dict__, which returns a dictionary of object’s attributes and their values.