Reference current module?

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

    #1

    Reference current module?

    Can I do:

    getattr(current _module, 'foo')

    where 'current_module ' is a handle the the one
    that the code is in? Just like

    getattr(self, 'foo')

    in a class, but for the current module instead?

    Thanks,

    Toby

    --
    Posted via a free Usenet account from http://www.teranews.com

  • Steven Bethard

    #2
    Re: Reference current module?

    Tobiah wrote:
    Can I do:
    >
    getattr(current _module, 'foo')
    >
    where 'current_module ' is a handle the the one
    that the code is in? Just like
    >
    getattr(self, 'foo')
    >
    in a class, but for the current module instead?
    You can try __import__() with __name__::
    >>foo = 42
    >>mod = __import__(__na me__)
    >>getattr(mod , 'foo')
    42

    STeVe

    Comment

    • Ben Finney

      #3
      Re: Reference current module?

      Tobiah <toby@tobiah.or gwrites:
      Can I do:
      >
      getattr(current _module, 'foo')
      >
      where 'current_module ' is a handle the the one
      that the code is in? Just like
      The python-list archives
      <URL:http://mail.python.org/pipermail/python-list/are your friend.

      Google is able to search within a site hierarchy by using search terms
      like the following:

      site:mail.pytho n.org/pipermail/python-list/ current module reference

      Searching there, I immediately find this brief thread:

      <URL:http://mail.python.org/pipermail/python-list/2006-August/398319.html>
      <URL:http://mail.python.org/pipermail/python-list/2006-August/398321.html>

      --
      \ "I hope if dogs ever take over the world, and they chose a |
      `\ king, they don't just go by size, because I bet there are some |
      _o__) Chihuahuas with some good ideas." -- Jack Handey |
      Ben Finney

      Comment

      • Gabriel Genellina

        #4
        Re: Reference current module?

        En Tue, 19 Jun 2007 16:34:50 -0300, Steven Bethard
        <steven.bethard @gmail.comescri bió:
        Tobiah wrote:
        >>
        >getattr(curren t_module, 'foo')
        >>
        >where 'current_module ' is a handle the the one
        >that the code is in? Just like
        >
        You can try __import__() with __name__::
        >
        >>foo = 42
        >>mod = __import__(__na me__)
        >>getattr(mod , 'foo')
        42
        A simple way would be using sys.modules[__name__].
        But I prefer using globals()['foo'] as it is simpler and does not assume
        additional requirements (like __name__ still being the same, or the module
        still available for importing).

        --
        Gabriel Genellina

        Comment

        • Tobiah

          #5
          Re: Reference current module?

          Gabriel Genellina wrote:
          En Tue, 19 Jun 2007 16:34:50 -0300, Steven Bethard
          <steven.bethard @gmail.comescri bió:
          >
          >Tobiah wrote:
          >>>
          >>getattr(curre nt_module, 'foo')
          >>>
          >>where 'current_module ' is a handle the the one
          >>that the code is in? Just like
          A simple way would be using sys.modules[__name__].
          But I prefer using globals()['foo'] as it is simpler and does not assume
          additional requirements (like __name__ still being the same, or the
          module still available for importing).
          >
          --Gabriel Genellina
          >
          In this case I really wanted a reference to
          the actual module, so that I could walk down
          from there in a sort of object mapping:

          def remote_call(cal l = 'foo.bar.other. thing'):
          destination_obj ect = sys.modules[__name__]
          for part in call.split('.') :
          destination_obj ect = getattr(destina tion_object, part)

          Tobiah

          --
          Posted via a free Usenet account from http://www.teranews.com

          Comment

          Working...