Module access from inside itself

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

    Module access from inside itself

    I'm writing a Python script which can be called from the command-line, and
    I want to use my module doc string as the CL help text, but I don't know
    how to access my module object from inside the module.

    I've tried searching for an answer, but haven't found anything. Please
    excuse me if I'm missing something simple, I'm a newbie to Python.

    I'm doing something like this:



    #!/usr/bin/python
    """Module doc string goes here.
    """

    import getopt, sys

    def MyFunction(args ):
        pass

    if __name__ == "__main__":
        opts, args = getop.getopt(sy s.argv[1:], [], ["help"])
        for opt in opts:
            if opt == "--help":
                print MY_MODULE.__doc __    # How do I get this?
            else:
                MyFunction(args )



    How do I get a reference to the module from inside the module? Is this
    the Pythonic way of generating help strings for CL scripts?


    Thanks,


    --
    Steven D'Aprano
  • Oren Tirosh

    #2
    Re: Module access from inside itself

    On Fri, Aug 15, 2003 at 04:20:43PM +0000, Steven wrote:[color=blue]
    > I'm writing a Python script which can be called from the command-line, and
    > I want to use my module doc string as the CL help text, but I don't know
    > how to access my module object from inside the module.[/color]

    You don't need the module object for this. MY_MODULE.__doc __ is the
    same as accessing __doc__ directly.

    If you really need the current module object use sys.modules[__name__]

    Oren

    Comment

    • Scott David Daniels

      #3
      Re: Module access from inside itself

      Steven wrote:
      [color=blue]
      > I'm writing a Python script which can be called from the command-line, and
      > I want to use my module doc string as the CL help text, but I don't know
      > how to access my module object from inside the module.[/color]

      How about this:

      # demo of accessing the docstring:

      """
      This is a test.

      Had this been a real document, it would have said something.
      """


      if __name__ == '__main__':
      import __main__
      print __main__.__doc_ _

      Comment

      • Bengt Richter

        #4
        Re: Module access from inside itself

        On Fri, 15 Aug 2003 16:20:43 GMT, Steven <[email protected] t.au> wrote:
        [color=blue]
        >I'm writing a Python script which can be called from the command-line, and
        >I want to use my module doc string as the CL help text, but I don't know
        >how to access my module object from inside the module.
        >
        >I've tried searching for an answer, but haven't found anything. Please
        >excuse me if I'm missing something simple, I'm a newbie to Python.
        >
        >I'm doing something like this:
        >
        >
        >
        >#!/usr/bin/python
        >"""Module doc string goes here.
        >"""
        >
        >import getopt, sys
        >
        >def MyFunction(args ):
        >    pass
        >
        >if __name__ == "__main__":
        >    opts, args = getop.getopt(sy s.argv[1:], [], ["help"])
        >    for opt in opts:
        >        if opt == "--help":
        >            print MY_MODULE.__doc __    # How do I get this?[/color]
                    print __doc__    # should get it
        [color=blue]
        >        else:
        >            MyFunction(args )
        >
        >
        >
        >How do I get a reference to the module from inside the module? Is this
        >the Pythonic way of generating help strings for CL scripts?
        >[/color]
        You are already inside the module, so an unqualified name will refer
        to module globals unless it's being used in some local scope that has
        a binding shadowing the global name.

        Regards,
        Bengt Richter

        Comment

        • Aahz

          #5
          Re: Module access from inside itself

          In article <pan.2003.08.16 .02.34.37.55574 [email protected] et.au>,
          Steven <[email protected] t.au> wrote:[color=blue]
          >
          >if __name__ == "__main__":
          >    opts, args = getop.getopt(sy s.argv[1:], [], ["help"])
          >    for opt in opts:
          >        if opt == "--help":
          >            print MY_MODULE.__doc __    # How do I get this?
          >        else:
          >            MyFunction(args )[/color]

          import __main__
          print __main__.__doc_ _
          --
          Aahz (aahz@pythoncra ft.com) <*> http://www.pythoncraft.com/

          This is Python. We don't care much about theory, except where it intersects
          with useful practice. --Aahz

          Comment

          Working...