Re: How to decide if a object is instancemethod?
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) == : # This should be always true pass # do something here What should type at ? 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to decide if a object is instancemethod?
On Thu, 15 Mar 2012 08:26:22 +1100, Ben Finney wrote: > Jon Clements 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to decide if a object is instancemethod?
Jon Clements 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: How to decide if a object is instancemethod?
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) == : # This should be always true > pass # do something here > > What should type at ? > > Thanks > Cosmia import inspect if inspect.ismethod(foo): # ... Will return True if foo is a bound method. hth Jon -- http://mail.python.org/mailman/listinfo/python-list
How to decide if a object is instancemethod?
class Foo(object): def bar(self): return 'Something' func = Foo().bar if type(func) == : # This should be always true pass # do something here What should type at ? Thanks Cosmia -- http://mail.python.org/mailman/listinfo/python-list