<p>I have a function in Python which is iterating over the attributes returned from <code>dir(obj)</code>, and I want to check to see if any of the objects contained within is a function, method, built-in function, etc.  Normally you could use <code>callable()</code> for this, but I don't want to include classes.  The best I've come up with so far is:</p>
<pre><code>isinstance(obj, (types.BuiltinFunctionType, types.FunctionType, types.MethodType))
</code></pre>
<p>Is there a more future-proof way to do this check?</p>
<p><strong>Edit:</strong> I misspoke before when I said: &quot;Normally you could use <code>callable()</code> for this, but I don't want to disqualify classes.&quot;  I actually <em>do</em> want to disqualify classes.  I want to match <em>only</em> functions, not classes.</p>

[{'answer_id': 74138, 'author': 'Jim', 'author_id': 8427, 'author_profile': 'https://Stackoverflow.com/users/8427', 'pm_score': 2, 'selected': False, 'text': '<pre><code>if hasattr(obj, \'__call__\'): pass\n</code></pre>\n\n<p>This also fits in better with Python\'s "duck typing" philosophy, because you don\'t really care <em>what</em> it is, so long as you can call it.</p>\n\n<p>It\'s worth noting that <code>callable()</code> is being removed from Python and is not present in 3.0.</p>\n'}, {'answer_id': 74295, 'author': 'dF.', 'author_id': 3002, 'author_profile': 'https://Stackoverflow.com/users/3002', 'pm_score': 3, 'selected': False, 'text': '<p>If you want to exclude classes and other random objects that may have a <code>__call__</code> method, and only check for functions and methods, these three functions in the <a href="http://docs.python.org/lib/module-inspect.html" rel="noreferrer"><code>inspect</code> module</a></p>\n\n<pre><code>inspect.isfunction(obj)\ninspect.isbuiltin(obj)\ninspect.ismethod(obj)\n</code></pre>\n\n<p>should do what you want in a future-proof way.</p>\n'}, {'answer_id': 75370, 'author': 'Matthieu', 'author_id': 9310, 'author_profile': 'https://Stackoverflow.com/users/9310', 'pm_score': 1, 'selected': False, 'text': "<p>Depending on what you mean by 'class':</p>\n\n<pre><code>callable( obj ) and not inspect.isclass( obj )\n</code></pre>\n\n<p>or:</p>\n\n<pre><code>callable( obj ) and not isinstance( obj, types.ClassType )\n</code></pre>\n\n<p>For example, results are different for 'dict':</p>\n\n<pre><code>&gt;&gt;&gt; callable( dict ) and not inspect.isclass( dict )\nFalse\n&gt;&gt;&gt; callable( dict ) and not isinstance( dict, types.ClassType )\nTrue\n</code></pre>\n"}, {'answer_id': 75507, 'author': 'Matthieu', 'author_id': 9310, 'author_profile': 'https://Stackoverflow.com/users/9310', 'pm_score': 5, 'selected': True, 'text': '<p>The inspect module has exactly what you want:</p>\n\n<pre><code>inspect.isroutine( obj )\n</code></pre>\n\n<p>FYI, the code is:</p>\n\n<pre><code>def isroutine(object):\n    """Return true if the object is any kind of function or method."""\n    return (isbuiltin(object)\n            or isfunction(object)\n            or ismethod(object)\n            or ismethoddescriptor(object))\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/74092', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/156/']