Re: How can a function know what module it's in?

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

    Re: How can a function know what module it's in?

    Some corrections, to highlight the depth of my confusion...

    On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
    doctest.testmod (mymodule)
    >
    This actually works fine if I'm importing the module (with the
    standard name) somewhere else
    Actually, it does not.
    I noticed that a function object has a __module__ attribute, that is
    a reference to the module the function is in.
    And no, it isn't; it's the NAME of the module the function is in. I'm
    not sure what good that does me. docstring.testm od does take an
    optional "name" parameter, but the documentation (at least in 2.5.2)
    does not explain what this parameter is used for. I tried using it
    thusly:

    doctest.testmod (name=_test.__m odule__)

    but that didn't work; it appears to still be testing the __main__
    module. (Perhaps the name parameter is only used to describe the
    module in the output, in which case, all I've accomplished here is
    getting doctest to lie.)
    I'm sure there is a magic identifier somewhere that lets a code get
    a reference to its own module, but I haven't been able to find it.
    Can someone share a clue?
    This question remains open. :)

    Thanks,
    - Joe

  • Rafe

    #2
    Re: How can a function know what module it's in?

    On Nov 12, 11:17 am, Joe Strout <j...@strout.ne twrote:
    Some corrections, to highlight the depth of my confusion...
    >
    On Nov 11, 2008, at 9:10 PM, Joe Strout wrote:
    >
       doctest.testmod (mymodule)
    >
    This actually works fine if I'm importing the module (with the  
    standard name) somewhere else
    >
    Actually, it does not.
    >
    I noticed that a function object has a __module__ attribute, that is  
    a reference to the module the function is in.
    >
    And no, it isn't; it's the NAME of the module the function is in.  I'm  
    not sure what good that does me.  docstring.testm od does take an  
    optional "name" parameter, but the documentation (at least in 2.5.2)  
    does not explain what this parameter is used for.  I tried using it  
    thusly:
    >
            doctest.testmod (name=_test.__m odule__)
    >
    but that didn't work; it appears to still be testing the __main__  
    module.  (Perhaps the name parameter is only used to describe the  
    module in the output, in which case, all I've accomplished here is  
    getting doctest to lie.)
    >
    I'm sure there is a magic identifier somewhere that lets a code get  
    a reference to its own module, but I haven't been able to find it.  
    Can someone share a clue?
    >
    This question remains open.  :)
    >
    Thanks,
    - Joe
    import sys
    this_module = sys.modules[__name__]

    sys.modules is a dictionary of all modules which have been imported
    during the current session. Since the module had to be imported to
    access it, it will be in there. '__name__' is available inside
    functions because it is in the module scope.

    Classes are a little more tricky because doing something like:
    this_module = sys.modules[self.__class__. __module__]
    will return a different module if the class is inherited in a
    different module (since the base class is __class__ now). However,
    putting a function at the module level (in the super-class module)
    should anchor the results (untested though).

    I'm not sure if this is the answer you need with regards to doctest,
    but it I think it answers the question in the subject of this thread.

    - Rafe

    Comment

    • Joe Strout

      #3
      Re: How can a function know what module it's in?

      On Nov 11, 2008, at 9:49 PM, Rafe wrote:
      >>I'm sure there is a magic identifier somewhere that lets a code get
      >>a reference to its own module, but I haven't been able to find it.
      >
      import sys
      this_module = sys.modules[__name__]
      Beautiful! Thanks very much. For the archives, here is my standard
      module-testing idiom now:

      def _test():
      import doctest, sys
      doctest.testmod (sys.modules[__name__])

      if __name__ == "__main__":
      _test()

      Now, when I execute the module directly, it will test itself; and if I
      need to test it from the outside (for example, under pdb) I can import
      the module and then run themodule._test ().

      To whom should I make the suggestion that this go into doctest docs?

      Cheers,
      - Joe

      Comment

      Working...