By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,630 Members | 1,313 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,630 IT Pros & Developers. It's quick & easy.

Testing if an object is a function

P: n/a
If I want to test if an object, x, is an integer, I can call
isinstance(x, int).

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.

--
Claus Tondering

Oct 5 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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>

Oct 5 '06 #2

P: n/a
Fredrik Lundh wrote:
if callable(x):
Perfect. Thank you.

--
Claus Tondering

Oct 5 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.