How to list all functions in an imported module?

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

    How to list all functions in an imported module?

    I can't figure out how to list all functions from an imported module.
    I searched Google, but all the answers I found didn't work. Did
    something change in Python 2.2, perhaps there's a new method of doing
    it?
  • Roy Smith

    #2
    Re: How to list all functions in an imported module?

    In article <889cbba0.04052 70506.3cd91d26@ posting.google. com>,
    klachemin@home. com (Kamilche) wrote:
    [color=blue]
    > I can't figure out how to list all functions from an imported module.
    > I searched Google, but all the answers I found didn't work. Did
    > something change in Python 2.2, perhaps there's a new method of doing
    > it?[/color]

    I would start with "dir (moduleName)"

    Comment

    • Mark McEahern

      #3
      Re: How to list all functions in an imported module?

      On Thu, 2004-05-27 at 08:06, Kamilche wrote:[color=blue]
      > I can't figure out how to list all functions from an imported module.
      > I searched Google, but all the answers I found didn't work. Did
      > something change in Python 2.2, perhaps there's a new method of doing
      > it?[/color]

      Try the inspect module.

      // m


      Comment

      • Peter Abel

        #4
        Re: How to list all functions in an imported module?

        klachemin@home. com (Kamilche) wrote in message news:<889cbba0. 0405270506.3cd9 1d26@posting.go ogle.com>...[color=blue]
        > I can't figure out how to list all functions from an imported module.
        > I searched Google, but all the answers I found didn't work. Did
        > something change in Python 2.2, perhaps there's a new method of doing
        > it?[/color]

        e.g the module os
        [color=blue][color=green][color=darkred]
        >>> import os,types
        >>> for k,v in os.__dict__.ite ms():[/color][/color][/color]
        .... if type(v) == types.BuiltinFu nctionType or\
        .... type(v) == types.BuiltinMe thodType or\
        .... type(v) == types.FunctionT ype or\
        .... type(v) == types.MethodTyp e:
        .... print '%-20s: %r' % (k,type(v))
        ....
        rename : <type 'builtin_functi on_or_method'
        lseek : <type 'builtin_functi on_or_method'>
        _get_exports_li st : <type 'function'>
        execle : <type 'function'>
        chmod : <type 'builtin_functi on_or_method'>
        execlp : <type 'function'>
        open : <type 'builtin_functi on_or_method'>
        write : <type 'builtin_functi on_or_method'>
        putenv : <type 'builtin_functi on_or_method'>
        fdopen : <type 'builtin_functi on_or_method'>
        _pickle_statvfs _result: <type 'function'>
        startfile : <type 'builtin_functi on_or_method'>
        umask : <type 'builtin_functi on_or_method'>
        system : <type 'builtin_functi on_or_method'>
        _execvpe : <type 'function'>
        getpid : <type 'builtin_functi on_or_method'>
        tmpnam : <type 'builtin_functi on_or_method'>
        dup : <type 'builtin_functi on_or_method'>
        spawnve : <type 'builtin_functi on_or_method'>
        getenv : <type 'function'>
        isatty : <type 'builtin_functi on_or_method'>
        execvpe : <type 'function'>
        dup2 : <type 'builtin_functi on_or_method'>
        read : <type 'builtin_functi on_or_method'>
        execvp : <type 'function'>
        popen3 : <type 'builtin_functi on_or_method'>
        _make_stat_resu lt : <type 'function'>
        execve : <type 'builtin_functi on_or_method'>
        utime : <type 'builtin_functi on_or_method'>
        execl : <type 'function'>
        chdir : <type 'builtin_functi on_or_method'>
        renames : <type 'function'>
        strerror : <type 'builtin_functi on_or_method'>
        remove : <type 'builtin_functi on_or_method'>
        fstat : <type 'builtin_functi on_or_method'>
        execv : <type 'builtin_functi on_or_method'>
        execlpe : <type 'function'>
        tempnam : <type 'builtin_functi on_or_method'>
        tmpfile : <type 'builtin_functi on_or_method'>
        popen4 : <type 'builtin_functi on_or_method'>
        popen2 : <type 'builtin_functi on_or_method'>
        stat : <type 'builtin_functi on_or_method'>
        abort : <type 'builtin_functi on_or_method'>
        close : <type 'builtin_functi on_or_method'>
        _exists : <type 'function'>
        spawnl : <type 'function'>
        makedirs : <type 'function'>
        access : <type 'builtin_functi on_or_method'>
        unsetenv : <type 'function'>
        mkdir : <type 'builtin_functi on_or_method'>
        spawnv : <type 'builtin_functi on_or_method'>
        listdir : <type 'builtin_functi on_or_method'>
        _pickle_stat_re sult : <type 'function'>
        lstat : <type 'builtin_functi on_or_method'>
        spawnle : <type 'function'>
        getcwd : <type 'builtin_functi on_or_method'>
        unlink : <type 'builtin_functi on_or_method'>
        _make_statvfs_r esult: <type 'function'>
        popen : <type 'builtin_functi on_or_method'>
        times : <type 'builtin_functi on_or_method'>
        pipe : <type 'builtin_functi on_or_method'>
        removedirs : <type 'function'>
        _exit : <type 'builtin_functi on_or_method'>
        rmdir : <type 'builtin_functi on_or_method'>[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        Regards
        Peter

        Comment

        • Peter Hansen

          #5
          Re: How to list all functions in an imported module?

          Peter Abel wrote:
          [color=blue]
          > klachemin@home. com (Kamilche) wrote in message news:<889cbba0. 0405270506.3cd9 1d26@posting.go ogle.com>...
          >[color=green]
          >>I can't figure out how to list all functions from an imported module.
          >>I searched Google, but all the answers I found didn't work. Did
          >>something change in Python 2.2, perhaps there's a new method of doing
          >>it?[/color]
          >
          >
          > e.g the module os
          >
          >[color=green][color=darkred]
          >>>>import os,types
          >>>>for k,v in os.__dict__.ite ms():[/color][/color]
          >
          > ... if type(v) == types.BuiltinFu nctionType or\
          > ... type(v) == types.BuiltinMe thodType or\
          > ... type(v) == types.FunctionT ype or\
          > ... type(v) == types.MethodTyp e:
          > ... print '%-20s: %r' % (k,type(v))[/color]

          Yuck... wouldn't using "callable(v )" be a lot nicer?

          -Peter

          Comment

          • Duncan Booth

            #6
            Re: How to list all functions in an imported module?

            Peter Hansen <peter@engcorp. com> wrote in
            news:o-idnZ6O7qVg5SvdR Vn-tA@powergate.ca :
            [color=blue]
            > Peter Abel wrote:
            >[color=green]
            >> klachemin@home. com (Kamilche) wrote in message
            >> news:<889cbba0. 0405270506.3cd9 1d26@posting.go ogle.com>...
            >>[color=darkred]
            >>>I can't figure out how to list all functions from an imported module.
            >>>I searched Google, but all the answers I found didn't work. Did
            >>>something change in Python 2.2, perhaps there's a new method of doing
            >>>it?[/color]
            >>
            >>
            >> e.g the module os
            >>
            >>[color=darkred]
            >>>>>import os,types
            >>>>>for k,v in os.__dict__.ite ms():[/color]
            >>
            >> ... if type(v) == types.BuiltinFu nctionType or\
            >> ... type(v) == types.BuiltinMe thodType or\
            >> ... type(v) == types.FunctionT ype or\
            >> ... type(v) == types.MethodTyp e:
            >> ... print '%-20s: %r' % (k,type(v))[/color]
            >
            > Yuck... wouldn't using "callable(v )" be a lot nicer?[/color]

            It depends whether he wants to include callable classes or really does want
            just functions.

            If he doesn't want callable classes, then a nicer way would be to use
            isinstance, which also gives you the option of factoring out the list of
            types:
            [color=blue][color=green][color=darkred]
            >>> FUNCTION_TYPES = (types.BuiltinF unctionType,[/color][/color][/color]
            types.BuiltinMe thodType,
            types.FunctionT ype,
            types.MethodTyp e,)
            [color=blue][color=green][color=darkred]
            >>> for k,v in os.__dict__.ite ms():[/color][/color][/color]
            if isinstance(v, FUNCTION_TYPES) :
            print '%-20s: %r' % (k,type(v))

            Comment

            • Kamilche

              #7
              Re: How to list all functions in an imported module?

              Thanks guys. It DOES work, as you stated... my problem was I had a
              module called the same name as a variable I was using, yeesh.

              I'm used to C, where you have to declare variables before use, and the
              compiler catches stuff like this. I might have to go back to a
              Hungarian-like naming conventions for variables, it appears.

              --Kamilche

              Comment

              Working...