[Tutor] dir() question: how do you know which are attributes and which are methods?

Magnus Lyckå magnus@thinkware.se
Mon May 26 12:24:02 2003


At 20:32 2003-05-25 -0400, R. Alan Monroe wrote:
>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...

Why don't you use help() instead of dir()?

 >>> class Dummy:
...     stuff = 0
...     def task(self):
...             pass
...

You did:

 >>> dir(Dummy)
['__doc__', '__module__', 'stuff', 'task']

You could do...

 >>> for name in dir(Dummy):
...     print name, eval('type(Dummy.%s)' % name)
...
__doc__ <type 'NoneType'>
__module__ <type 'str'>
stuff <type 'int'>
task <type 'instance method'>

...to get some more help, but most of the time you are better off doing:

 >>> help(Dummy)
Help on class Dummy in module __main__:
class Dummy
  |  Methods defined here:
  |
  |  task(self)
  |
  |  ----------------------------------------------------------------------
  |  Data and non-method functions defined here:
  |
  |  __doc__ = None
  |
  |  __module__ = '__main__'
  |
  |  stuff = 0
 >>>


--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program