Know if a object member is a method
steven at REMOVE.THIS.cybersource.com.au
Mon Sep 1 11:16:56 CEST 2008
On Mon, 01 Sep 2008 10:43:25 +0200, Luca wrote:
> Hi all.
> I think this is a newbie question... what is the best method to know if
> a property of an object is a function?
> I'm thinking something as
> if type(obj.methodName)==???
> Can someone help me?
That's not quite as easy as you might think. In my testing, calling
type(obj.methodName) can give any of the following:
There may be others as well.
Possibly all you really need is to check if the attribute is callable
without caring what type of method or function it is:
In my opinion, that's the best way.
If you are sure that the method won't have side-effects or bugs:
... print "Not a method"
(But how can you be sure?)
If you insist on an explicit test, try this:
type(obj.methodName) == new.instancemethod
>>> isinstance(obj.methodName, new.instancemethod)
More information about the Python-list