[Tutor] dir() question: how do you know which are attributes
and which are methods?
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Tue May 27 14:47:02 2003
On Sun, 25 May 2003, R. Alan Monroe wrote:
> Subject says it all...
>
> example
>
>
> class dummy:
> stuff = 0
> def task():
> pass
>
> >>> dir(dummy)
> ['__doc__', '__module__', 'stuff', 'task']
>
> How do I find out, when working with a new class I'm not familiar with,
> how many attributes it has, and how many methods? dir() is a bit too
> generic because both look identical in the list...
Hi Alan,
(Well, technically, a method in Python is an attribute. *grin*)
You may find the 'inspect' module useful to do digging through the class
definitions:
http://www.python.org/doc/lib/inspect-types.html
###
>>> class dummy:
... stuff = 0
... def task(self):
... pass
...
>>> import inspect
>>> inspect.getmembers(dummy, inspect.ismethod)
[('task', <unbound method dummy.task>)]
>>>
>>>
>>> class dummy2(dummy):
... def task2(self): pass
...
>>> inspect.getmembers(dummy2, inspect.ismethod)
[('task', <unbound method dummy2.task>),
('task2', <unbound method dummy2.task2>)]
###
Hope this helps. Good luck to you!