Search code examples
pythoncallable

Python: listing callable objects?


I am trying to list only callable objects but I seem to be doing something wrong. For example,

>>> [m for m in dir({}) if callable(getattr({},m))]
['__class__', '__cmp__', '__contains__', '__delattr__', '__delitem__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'clear', 'copy', 'fromkeys', 'get', 'has_key', 'items', 'iteritems', 'iterkeys', 'itervalues', 'keys', 'pop', 'popitem', 'setdefault', 'update', 'values']

However, this is wrong, for example {}.__doc__ is clearly a string, etc. What is wrong with my list comprehension


Solution

  • Your code as written is the best Python has to offer. You mention it being wrong because of __doc__, but your list doesn't include __doc__. I think you already have working code.