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


Groups > comp.lang.python > #21597 > unrolled thread

How to decide if a object is instancemethod?

Started byCosmia Luna <cosmius@gmail.com>
First post2012-03-14 06:28 -0700
Last post2012-03-26 11:44 +0200
Articles 5 — 5 participants

Back to article view | Back to comp.lang.python


Contents

  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

#21597 — How to decide if a object is instancemethod?

FromCosmia Luna <cosmius@gmail.com>
Date2012-03-14 06:28 -0700
SubjectHow to decide if a object is instancemethod?
Message-ID<8815977.3878.1331731738209.JavaMail.geo-discussion-forums@vbux23>
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

[toc] | [next] | [standalone]


#21600

FromJon Clements <joncle@googlemail.com>
Date2012-03-14 06:57 -0700
Message-ID<389400.5582.1331733455084.JavaMail.geo-discussion-forums@vbiz13>
In reply to#21597
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

[toc] | [prev] | [next] | [standalone]


#21627

FromBen Finney <ben+python@benfinney.id.au>
Date2012-03-15 08:26 +1100
Message-ID<87ty1qg9hd.fsf@benfinney.id.au>
In reply to#21600
Jon Clements <joncle@googlemail.com> writes:

> import inspect
> if inspect.ismethod(foo):
>    # ...
>
> Will return True if foo is a bound method.

But under what other conditions will it return True? The name suggests
that *any* method – static method, class method, bound method, unbound
method – will also result in True.

The documentation says only “instance method”, though. Confusing :-(

-- 
 \     “Airports are ugly. Some are very ugly. Some attain a degree of |
  `\        ugliness that can only be the result of a special effort.” |
_o__)             —Douglas Adams, _The Long Dark Tea-Time Of The Soul_ |
Ben Finney

[toc] | [prev] | [next] | [standalone]


#21632

FromSteven D'Aprano <steve+comp.lang.python@pearwood.info>
Date2012-03-14 23:30 +0000
Message-ID<4f612a12$0$29981$c3e8da3$5496439d@news.astraweb.com>
In reply to#21627
On Thu, 15 Mar 2012 08:26:22 +1100, Ben Finney wrote:

> Jon Clements <joncle@googlemail.com> writes:
> 
>> import inspect
>> if inspect.ismethod(foo):
>>    # ...
>>
>> Will return True if foo is a bound method.
> 
> But under what other conditions will it return True? The name suggests
> that *any* method – static method, class method, bound method, unbound
> method – will also result in True.
> 
> The documentation says only “instance method”, though. Confusing :-(


Bound and unbound methods are instance methods. To be precise, the 
"method" in "(un)bound method" stands for instance method, and the 
difference between the two in Python 2.x is a flag on the method object. 
(Unbound methods are gone in Python 3.)

Class and static methods are not instance methods. I suppose it is 
conceivable that you could have an unbound class method in theory, but I 
can't see any way to actually get one in practice.

In Python, and probably most languages, a bare, unadorned "method" is 
implied to be an instance method; "instance method" is (possibly) a 
retronym to distinguish them from other, newer(?), types of method.

http://en.wikipedia.org/wiki/Retronym



-- 
Steven

[toc] | [prev] | [next] | [standalone]


#22168

FromJean-Michel Pichavant <jeanmichel@sequans.com>
Date2012-03-26 11:44 +0200
Message-ID<mailman.989.1332755057.3037.python-list@python.org>
In reply to#21600
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web