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?

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <prvs=42574d81d=jeanmichel@sequans.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.068
X-Spam-Evidence '*H*': 0.86; '*S*': 0.00; 'wednesday,': 0.07; 'cc:addr:python-list': 0.15; 'foo(object):': 0.16; 'func': 0.16; 'luna': 0.16; 'subject:object': 0.16; 'utc,': 0.16; 'def': 0.20; 'wrote:': 0.21; 'method.': 0.22; 'header:In-Reply-To:1': 0.22; 'header:User-Agent:1': 0.23; 'possibly': 0.23; 'import': 0.24; 'cc:no real name:2**0': 0.26; 'subject:How': 0.27; 'cc:addr:python.org': 0.27; 'pass': 0.28; 'foo': 0.29; 'subject:skip:i 10': 0.29; 'class': 0.29; 'cc:2**0': 0.31; 'thanks': 0.34; 'subject:?': 0.34; 'should': 0.35; 'something': 0.38; 'type': 0.62; 'better': 0.63; 'header:Received:2': 0.63; 'alternative': 0.65; 'here': 0.66; 'march': 0.67; '2012': 0.69; 'bound': 0.72; 'received:194': 0.72; 'inspect': 0.91
X-IronPort-AV E=Sophos;i="4.75,319,1330902000"; d="scan'208";a="275063"
X-Virus-Scanned amavisd-new at zimbra.sequans.com
Date Mon, 26 Mar 2012 11:44:01 +0200
From Jean-Michel Pichavant <jeanmichel@sequans.com>
User-Agent Mozilla-Thunderbird 2.0.0.24 (X11/20100328)
MIME-Version 1.0
To Jon Clements <joncle@googlemail.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>
In-Reply-To <389400.5582.1331733455084.JavaMail.geo-discussion-forums@vbiz13>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
Cc python-list@python.org
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.989.1332755057.3037.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1332755057 news.xs4all.nl 6940 [2001:888:2000:d::a6]:49537
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:22168

Show key headers only | 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