listing attributes
Peter Hansen
peter at engcorp.com
Tue Feb 14 09:53:37 EST 2006
Steven D'Aprano wrote:
> On Mon, 13 Feb 2006 22:18:56 -0500, Peter Hansen wrote:
>>Thomas Girod wrote:
>>>I'm trying to get a list of attributes from a class. The dir() function
>>>seems to be convenient, but unfortunately it lists to much - i don't
>>>need the methods, neither the built-in variables.
>>>
>>>In fact, all my variables are referencing to objects of the same type.
>>>Can anyone suggest me a way to get this list of variables ?
>>
>>Does the __dict__ attribute help you? (Try viewing obj.__dict__ at the
>>interpreter prompt and see if it has what you expect.
>>obj.__dict__.keys() would be just the names of those attributes.)
>
>>>>Parrot.__dict__.keys()
>
> ['__module__', 'ATTR', 'method', '__dict__', '__weakref__', '__doc__']
>
> So I guess the answer to that question is, while __dict__ gives less
> information than dir, it still gives too much.
I was making the perhaps mistaken assumption that the OP was another of
those who meant "object" when he said "class" (though I could perhaps
have asked that explicitly).
If that were the case, I think __dict__ could be what he wanted. (I'm
still not sure what he wanted, even after reading his response, since I
can't see what his classes or objects look like...)
>>> class Parrot(object):
... ATTR = None
... def method(self):
... return None
...
>>> polly = Parrot()
>>> polly.foo = '1'
>>> polly.bar = 'baz'
>>> polly.__dict__
{'foo': '1', 'bar': 'baz'}
-Peter
More information about the Python-list
mailing list