Method by name lookup

Will Hartung will.hartung at havasint.com
Thu Nov 2 13:28:31 EST 2000


I'm writing something that needs to get method functions from a class by
name.

The trivial path is something like:

funcitem = instance.__class__.__dict__.get('funcname', None)

The issue is that this only works for local methods, not inherited methods.

I can't use something like:

funcitem = instance.funcname

because funcname may not exist in the object.

So, not being motivated enough to crawl through the inhertiance graph to see
if a function is implemented, I was curious as to whether  there is some
function that I might be able to call to do the resolution for me. I was
hoping for something more elegant than:

funcitem = eval('instance.funcname')

Seems a touch expensive, but perhaps not. It does seem a little too shotgun
for me, though.

As an extra bonus, is there anyplace one can get all of the function names
associated with an instance?

Thanx!

Will Hartung
(will.hartung at havasint.com)






More information about the Python-list mailing list