list of all functions defined in the current module

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

    #1

    list of all functions defined in the current module

    Hi,

    How can I get the list of all the functions defined in the current module?
  • anton muhin

    #2
    Re: list of all functions defined in the current module

    Fernando Rodriguez wrote:[color=blue]
    > Hi,
    >
    > How can I get the list of all the functions defined in the current module?[/color]

    Here's what I managed to do:

    <listfunc.py>
    import inspect

    def listfunc():
    me = __import__(insp ect.getmodulena me(__file__))
    for name in dir(me):
    obj = getattr(me, name)
    if inspect.isfunct ion(obj):
    yield obj

    def foo(): pass

    def bar(): pass
    </lisfunc.py>

    <d.py>
    import listfunc

    for func in listfunc.listfu nc():
    print func
    </d.py>

    It prints:
    <function bar at 0x008ECAF0>
    <function foo at 0x008ECAB0>
    <function listfunc at 0x008F3430>

    regards,
    anton.

    Comment

    • Gerhard Häring

      #3
      Re: list of all functions defined in the current module

      Fernando Rodriguez wrote:[color=blue]
      > Hi,
      >
      > How can I get the list of all the functions defined in the current module?[/color]

      If you have

      def getfunctions(mo dule):
      import types
      l = []
      for key, value in module.__dict__ .items():
      if type(value) is FunctionType:
      l.append(value)
      return l

      Then you can call this function with getfunctions(sy s.modules[__name__])
      to get a list of functions in the current module.

      Perhaps you could also use the builtin 'inspect' module for this task.

      -- Gerhard


      Comment

      • David Eppstein

        #4
        Re: list of all functions defined in the current module

        In article <mailman.957.10 69416870.702.py thon-list@python.org >,
        Gerhard Haring <gh@ghaering.de > wrote:
        [color=blue]
        > Fernando Rodriguez wrote:[color=green]
        > > Hi,
        > >
        > > How can I get the list of all the functions defined in the current module?[/color]
        >
        > If you have
        >
        > def getfunctions(mo dule):
        > import types
        > l = []
        > for key, value in module.__dict__ .items():
        > if type(value) is FunctionType:
        > l.append(value)
        > return l
        >
        > Then you can call this function with getfunctions(sy s.modules[__name__])
        > to get a list of functions in the current module.
        >
        > Perhaps you could also use the builtin 'inspect' module for this task.[/color]

        If you just want a list of a module's globals, what's wrong with
        dir(modulename)
        ?

        Of course it will tell you about globals that are not functions (e.g.
        classes) but maybe that's what the original poster wanted anyway.

        --
        David Eppstein http://www.ics.uci.edu/~eppstein/
        Univ. of California, Irvine, School of Information & Computer Science

        Comment

        Working...