Testing if an object is a function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Claus Tondering

    #1

    Testing if an object is a function

    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

  • Fredrik Lundh

    #2
    Re: Testing if an object is a function

    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>



    Comment

    • Claus Tondering

      #3
      Re: Testing if an object is a function

      Fredrik Lundh wrote:
      if callable(x):
      Perfect. Thank you.

      --
      Claus Tondering

      Comment

      Working...