I've got a list of things, of which some can also be functions. If it is a function I would like to execute it. For this I do a type-check. This normally works for other types, like str, int or float. But for a function it doesn't seem to work:
>>> def f():
... pass
...
>>> type(f)
>>> if type(f) == function: print 'It is a function!!'
...
Traceback (most recent call last):
File "", line 1, in
NameError: name 'function' is not defined
>>>
Does anybody know how I can check for a function type?
解决方案
Don't check types, check actions. You don't actually care if it's a function (it might be a class instance with a __call__ method, for example) - you just care if it can be called. So use callable(f).