Source From Here
QuestionI have a variable, x, and I want to know whether it is pointing to a function or not.
I had hoped I could do something like:
- >>> isinstance(x, function)
- Traceback (most recent call last):
- File "<stdin>", line 1, in ?
- NameError: name 'function' is not defined
- >>> type(x)
- <type 'function'>
If this is for Python 2.x or for Python 3.2+, you can use callable(). It used to be deprecated, but is now undeprecated, so you can use it again. You can read the discussion here: http://bugs.python.org/issue10518. You can do this with:
- callable(obj)
- hasattr(obj, '__call__')
- >>> isinstance(open, types.FunctionType)
- False
- >>> callable(open)
- True