How to decide if a object is instancemethod?

Jean-Michel Pichavant jeanmichel at sequans.com
Mon Mar 26 11:44:01 CEST 2012


Jon Clements wrote:
> On Wednesday, 14 March 2012 13:28:58 UTC, Cosmia Luna  wrote:
>   
>> class Foo(object):
>>     def bar(self):
>>         return 'Something'
>>
>> func = Foo().bar
>>
>> if type(func) == <type 'instancemethod'>: # This should be always true
>>     pass # do something here
>>
>> What should type at <type 'instancemethod'>?
>>
>> Thanks
>> Cosmia
>>     
>
> import inspect
> if inspect.ismethod(foo):
>    # ...
>
> Will return True if foo is a bound method.
>
> hth
>
> Jon
>   
another alternative :

import types

if type(func) == types.MethodType:
    pass

or possibly better

if isinstance(func, types.MethodType):
    pass


JM



More information about the Python-list mailing list