How can one check if a variable is an instance method or not? I'm using python 2.5.
Something like this:
class Test:
def method(self):
pass
assert is_instance_method(Test().method)
How can one check if a variable is an instance method or not? I'm using python 2.5.
Something like this:
class Test:
def method(self):
pass
assert is_instance_method(Test().method)
If you want to know if it is precisely an instance method use the following function. (It considers methods that are defined on a metaclass and accessed on a class class methods, although they could also be considered instance methods)
Usually checking for that is a bad idea. It is more flexible to be able to use any callable() interchangeably with methods.
inspect.ismethod
is what you want to find out if you definitely have a method, rather than just something you can call.callable
is true if the argument if the argument is a method, a function (includinglambda
s), an instance with__call__
or a class.Methods have different properties than functions (like
im_class
andim_self
). So you want