I want to know if a function is defined for an instance of a class
class c(object):
def func(self): pass
cc = c()
>>> is_function_defined_for_instance(cc,'func')
True
>>> is_function_defined_for_instance(cc,'cnuf')
False
my cleanest attempts at the function is:
def is_function_defined_for_instance(instance,function):
return callable(getattr(instance,function,None))
# returns True iff function is defined for class of instance (not super classes)
def is_function_defined_for_instance(instance,function):
return bool(instance.__class__.__dict__.get(function))
is there a built-in or a nicer way to accomplish what I’m looking for?
or you could use
inspectmodule: