Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #22168

Re: How to decide if a object is instancemethod?

Date 2012-03-26 11:44 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
Subject Re: How to decide if a object is instancemethod?
References <8815977.3878.1331731738209.JavaMail.geo-discussion-forums@vbux23> <389400.5582.1331733455084.JavaMail.geo-discussion-forums@vbiz13>
Newsgroups comp.lang.python
Message-ID <mailman.989.1332755057.3037.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

How to decide if a object is instancemethod? Cosmia Luna <cosmius@gmail.com> - 2012-03-14 06:28 -0700
  Re: How to decide if a object is instancemethod? Jon Clements <joncle@googlemail.com> - 2012-03-14 06:57 -0700
    Re: How to decide if a object is instancemethod? Ben Finney <ben+python@benfinney.id.au> - 2012-03-15 08:26 +1100
      Re: How to decide if a object is instancemethod? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2012-03-14 23:30 +0000
    Re: How to decide if a object is instancemethod? Jean-Michel Pichavant <jeanmichel@sequans.com> - 2012-03-26 11:44 +0200

csiph-web