name of client module

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

    name of client module

    Q1: When a module is imported, is there any way for the module to
    determine the name of the client code's module?

    Q2: My understanding is that the code in a module is executed only on
    the first import of that module. Is there any way to have a hook
    invoked on subsequent imports, and for that hook (as in Q1) to determine
    the name of the client module?
  • Nick Stinemates

    #2
    Re: name of client module

    Jeff Schwab wrote:
    Q1: When a module is imported, is there any way for the module to
    determine the name of the client code's module?
    >
    Why would you ever want to do this?
    Q2: My understanding is that the code in a module is executed only on
    the first import of that module. Is there any way to have a hook
    invoked on subsequent imports, and for that hook (as in Q1) to determine
    the name of the client module?
    >
    Why would you ever want to do this?

    I don't really understand why you wouldn't want to do the following:

    import foo
    foo.exec()



    --
    =============== ===
    Nick Stinemates (nick@stinemate s.org)


    AIM: Nick Stinemates
    MSN: nickstinemates@ hotmail.com
    Yahoo: nickstinemates@ yahoo.com
    =============== ===


    Comment

    • Jeff Schwab

      #3
      Re: name of client module

      Nick Stinemates wrote:
      Jeff Schwab wrote:
      >Q1: When a module is imported, is there any way for the module to
      >determine the name of the client code's module?
      >>
      Why would you ever want to do this?
      >Q2: My understanding is that the code in a module is executed only on
      >the first import of that module. Is there any way to have a hook
      >invoked on subsequent imports, and for that hook (as in Q1) to determine
      >the name of the client module?
      >>
      Why would you ever want to do this?
      So that the imported module can implement functions that return
      information about the client module, as a form of introspection.
      Suppose I want to know whether I'm the main module, and I don't want to
      write __name__ == '__main__'; it would be nice if I could import a
      module and call a method to tell me whether I'm __main__:

      import modinfo

      if modinfo.main():
      print("Hello, world")
      I don't really understand why you wouldn't want to do the following:
      >
      import foo
      foo.exec()
      I'm not saying I don't want to do that. I'm saying that, in addition to
      what you've written, I want foo to know it's being imported, and by whom.

      Comment

      • Jeff Schwab

        #4
        Re: name of client module

        Nick Stinemates wrote:
        >I'm not saying I don't want to do that. I'm saying that, in addition to
        >what you've written, I want foo to know it's being imported, and by whom.
        Please don't snip so much.
        You're still not explaining a real example of what this could be used for.
        Why would you say something like that? I told you *exactly* what I
        wanted to use it for. See Berwyn's post on the recent thread "Double
        underscores -- ugly?" He suggests that a suitable replacement for "if
        __name__ == '__main__'" might be "if sys.main()". I was looking for a
        way to implement that kind of function. Just like I told you when you
        asked me. You snipped it, along with most of the post.
        Oh well, here's an example of an implementation of what you want to do.
        Thanks.

        Comment

        • Ben Finney

          #5
          Re: name of client module

          Nick Stinemates <nick@stinemate s.orgwrites:
          Ah, I snipped because I was only replying to that specific part and
          thought there was an archive of the rest. If that is unconventional
          I'll stop.
          Please continue snipping the parts that aren't relevant to your reply.
          The convention in this forum is trimmed-quote, inline-reply.

          I think the complaint in this case might have been the "you snipped
          bits that *were* relevant" failure mode :-)

          --
          \ "Madness is rare in individuals, but in groups, parties, |
          `\ nations and ages it is the rule." -- Friedrich Nietzsche |
          _o__) |
          Ben Finney

          Comment

          • Jeff Schwab

            #6
            Re: name of client module

            Ben Finney wrote:
            Nick Stinemates <nick@stinemate s.orgwrites:
            >
            >Ah, I snipped because I was only replying to that specific part and
            >thought there was an archive of the rest. If that is unconventional
            >I'll stop.
            >
            Please continue snipping the parts that aren't relevant to your reply.
            The convention in this forum is trimmed-quote, inline-reply.
            >
            I think the complaint in this case might have been the "you snipped
            bits that *were* relevant" failure mode :-)
            What he said.

            Comment

            Working...