type(foo) == function ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tom Plunket

    #1

    type(foo) == function ?

    I'd like to figure out if a given parameter is a function or not.

    E.g.
    >>type(1)
    <type 'int'>
    >>type(1) == int
    True

    implies:
    >>def foo():
    .... pass
    ....
    >>type(foo)
    <type 'function'>
    >>type(foo) == function
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'function' is not defined

    Is there a way I can know if 'foo' is a function?

    thanks,
    -tom!
  • Chris Mellon

    #2
    Re: type(foo) == function ?

    On 11/29/06, Tom Plunket <tomas@fancy.or gwrote:
    I'd like to figure out if a given parameter is a function or not.
    >
    E.g.
    >
    >type(1)
    <type 'int'>
    >type(1) == int
    True
    >
    implies:
    >
    >def foo():
    ... pass
    ...
    >type(foo)
    <type 'function'>
    >type(foo) == function
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    NameError: name 'function' is not defined
    >
    Is there a way I can know if 'foo' is a function?
    >
    >>def foo():
    .... pass
    ....
    >>from inspect import isfunction
    >>isfunction(fo o)
    True
    >>>

    But you probably want the callable() builtin instead.

    Comment

    • Erik Max Francis

      #3
      Re: type(foo) == function ?

      Hi Tom.

      Tom Plunket wrote:
      I'd like to figure out if a given parameter is a function or not.
      >
      E.g.
      >
      >>>type(1)
      <type 'int'>
      >>>type(1) == int
      True
      >
      implies:
      >
      >>>def foo():
      ... pass
      ...
      >>>type(foo)
      <type 'function'>
      >>>type(foo) == function
      Traceback (most recent call last):
      File "<stdin>", line 1, in ?
      NameError: name 'function' is not defined
      >
      Is there a way I can know if 'foo' is a function?
      The type of a function is types.FunctionT ype:
      >>import types
      >>types.Functio nType
      <type 'function'>
      >>def f(): pass
      ....
      >>type(f)
      <type 'function'>
      >>type(f) is types.FunctionT ype
      True

      However, this doesn't take into account methods (MethodType and
      UnboundMethodTy pe). In dynamically-typed languages in general, explicit
      typechecks are not a good idea, since they often preclude user-defined
      objects from being used. Instead, try performing the call and catch the
      resulting TypeError:
      >>f = 'asdf'
      >>try:
      .... f()
      .... except TypeError:
      .... print "oops, f is not callable"
      ....
      oops, f is not callable

      --
      Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
      San Jose, CA, USA && 37 20 N 121 53 W && AIM, Y!M erikmaxfrancis
      There are not fifty ways of fighting, there is only one: to be the
      conqueror. -- Andrew Malraux, 1937

      Comment

      • Fredrik Lundh

        #4
        Re: type(foo) == function ?

        Tom Plunket wrote:
        I'd like to figure out if a given parameter is a function or not.


        </F>

        Comment

        • Tom Plunket

          #5
          Re: type(foo) == function ?

          Erik Max Francis wrote:
          In dynamically-typed languages in general, explicit typechecks are not
          a good idea, since they often preclude user-defined objects from being
          used. Instead, try performing the call and catch the resulting
          TypeError:
          Good point, although I need to figure out if the thing can be called
          without calling it, so I can build an appropriate UI. Basically I
          expect three types of things in the 'value' field of the top-level
          dictionary. The three sorts of things that I will deal with in the UI
          are callable things (e.g. functions, for which Chris Mellon reminds me
          about callable()), mappings (e.g. dictionaries, used similarly to the
          top-level one), and sequences of strings.

          So I think callable() works for me in the general case, but now
          trawling the documentation in that area I'm not sure how I can tell if
          something is a mapping or if it's a sequence.

          The gist of the UI generation may be envisioned as:

          key is the name that gets assigned to the control.
          value indicates that the UI element is a:
          "group box" if the value is a mapping
          series of "radio buttons" if the value is a sequence of strings
          "check box" if the value is a function

          ....I've still gotta figure out the exact API, this is for a plugin
          sort of system that'll be used by the manually-driven version of the
          build process and this data is explicitly to build the UI for the
          various tools that are available.

          thanks,
          -tom!

          Comment

          • Chris Mellon

            #6
            Re: type(foo) == function ?

            On 11/29/06, Tom Plunket <tomas@fancy.or gwrote:
            Erik Max Francis wrote:
            >
            In dynamically-typed languages in general, explicit typechecks are not
            a good idea, since they often preclude user-defined objects from being
            used. Instead, try performing the call and catch the resulting
            TypeError:
            >
            Good point, although I need to figure out if the thing can be called
            without calling it, so I can build an appropriate UI. Basically I
            expect three types of things in the 'value' field of the top-level
            dictionary. The three sorts of things that I will deal with in the UI
            are callable things (e.g. functions, for which Chris Mellon reminds me
            about callable()), mappings (e.g. dictionaries, used similarly to the
            top-level one), and sequences of strings.
            >
            So I think callable() works for me in the general case, but now
            trawling the documentation in that area I'm not sure how I can tell if
            something is a mapping or if it's a sequence.
            >
            The gist of the UI generation may be envisioned as:
            >
            key is the name that gets assigned to the control.
            value indicates that the UI element is a:
            "group box" if the value is a mapping
            series of "radio buttons" if the value is a sequence of strings
            "check box" if the value is a function
            >
            ...I've still gotta figure out the exact API, this is for a plugin
            sort of system that'll be used by the manually-driven version of the
            build process and this data is explicitly to build the UI for the
            various tools that are available.
            >
            Since a sequence can be viewed as a special case of a mapping (a dict
            with integer keys) I don't think there's a good general case way to
            tell.

            You could try indexing it with a string, a sequence will throw a
            TypeError, while a mapping will give you a result or a KeyError.

            You could also just rely on isinstance() checks.

            Comment

            • Mathias Panzenboeck

              #7
              Re: type(foo) == function ?

              Chris Mellon wrote:
              On 11/29/06, Tom Plunket <tomas@fancy.or gwrote:
              >I'd like to figure out if a given parameter is a function or not.
              >>
              >E.g.
              >>
              >>type(1)
              ><type 'int'>
              >>type(1) == int
              >True
              >>
              >implies:
              >>
              >>def foo():
              >... pass
              >...
              >>type(foo)
              ><type 'function'>
              >>type(foo) == function
              >Traceback (most recent call last):
              > File "<stdin>", line 1, in ?
              >NameError: name 'function' is not defined
              >>
              >Is there a way I can know if 'foo' is a function?
              >>
              >
              >>>def foo():
              ... pass
              ...
              >>>from inspect import isfunction
              >>>isfunction(f oo)
              True
              >>>>
              >
              >
              But you probably want the callable() builtin instead.
              >
              This builtin will be removed in python 3.0!

              Comment

              • Fredrik Lundh

                #8
                Re: type(foo) == function ?

                Mathias Panzenboeck wrote:
                This builtin will be removed in python 3.0!
                that's far from clear (since the replacement approach, "just call it",
                simply doesn't work). and if you track the Py3K discussions, you'll
                notice current threads about *more* support for interface testing, not
                less support.

                </F>

                Comment

                Working...