[Tutor] viewing attributes of objects in a class
Don Arnold
Don Arnold" <darnold02@sprynet.com
Sat Mar 8 10:58:04 2003
----- Original Message -----
From: "reavey" <reavey@nep.net>
To: <tutor@python.org>
Sent: Sunday, March 09, 2003 9:31 AM
Subject: [Tutor] viewing attributes of objects in a class
> Mike Reavey wrote:
>
> >>> class = Object:
> pass
> >>>personA = Object()
> >>> personA.name = "Ann"
> >>>personA.age = 31
> is there a list of this object with its attributes which can be retrieved,
> like personA.inspect?
> TIA
> re-v
If you're just running interactively and want to see the object's
attributes, you can use the built in dir() function:
class MyClass:
pass
personA = MyClass()
personA.name = "Ann"
personA.age = 31
print dir(personA)
>>> ['__doc__', '__module__', 'age', 'name']
Otherwise, you can access the object's __dict__, which is the dictionary
that stores its attributes:
for attrib in personA.__dict__.keys():
print '%s = %s' % (attrib, personA.__dict__[attrib])
>>> age = 31
>>> name = Ann
HTH,
Don