Testing if an object is a function

Fredrik Lundh fredrik at pythonware.com
Thu Oct 5 07:04:05 EDT 2006


Claus Tondering wrote:

> But what do I do if I want to test if x is a function?
>
> I can do this:
>
>     if isinstance(x, type(lambda: None)): ...
>
> But it does not seem very elegant to me.
>
> Surely there is a simpler way to specify a type object that is the type
> of a function.

    if callable(x):
        ...

(if you really want a function object, and not anything that's likely to be possible
to call, see the "types" module)

</F> 






More information about the Python-list mailing list