How to list currently defined classes, methods etc
Kent Johnson
kent37 at tds.net
Fri Dec 2 15:33:11 EST 2005
Deep wrote:
> I have been looking a bit and am stuck at this point.
>
> Given a string, how do i find what is the string bound to.
> Let me give an example.
>
> def deep():
> print "Hello"
>
> now inspect.ismethod(deep) returns true. (As it should).
> But if I am trying to make a list of all bound methods), i use
> dir(), which is a list of strings. I get the string "deep" from this
> list.
Look it up in the globals() dict:
>>> def deep():
... print 'Hello'
...
>>> globals()['deep']
<function deep at 0x008ECF70>
> How do I obtain the reference to the method it is bound to.
> The same problem can be extended to attributes and classes.
Use getattr() to inspect classes and instances:
>>> class deeper:
... def deepest(self):
... print 'goodbye'
...
>>> getattr(deeper, 'deepest')
<unbound method deeper.deepest>
>>> d=deeper()
>>> getattr(d, 'deepest')
<bound method deeper.deepest of <__main__.deeper instance at 0x00A41350>>
Kent
More information about the Python-list
mailing list