Know if a object member is a method
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Mon Sep 1 08:18:16 EDT 2008
On Mon, 01 Sep 2008 11:45:36 +0200, Luca asked about recognizing methods:
> What is the best way to do this? The "most pythonic"?
That depends on why you are doing it, and what you want to do with the
information once you've found it.
If you are experimenting in the interactive interpreter, the easiest way
is simply call the method and see what happens:
obj.methodName() # call the method
If it succeeds, then it is some sort of callable, a method or a function
or something more unusual.
If you fear side-effects, then use:
callable(obj.methodName)
Alternatively, you can read the docs:
help(obj)
help(obj.methodName)
If your aim is to write something like a debugger, profiler, or some
other application that needs to inspect arbitrary objects and work out
what they do, then you probably should be using:
isinstance(obj.methodName, new.instancemethod)
But remember that not all callable attributes are instancemethods!
There is no one right way of doing this.
Hope this helps.
--
Steven
More information about the Python-list
mailing list