By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,190 Members | 1,412 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,190 IT Pros & Developers. It's quick & easy.

name of client module

P: n/a
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?
Feb 19 '08 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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 (ni**@stinemates.org)
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: ni************@hotmail.com
Yahoo: ni************@yahoo.com
==================
Feb 19 '08 #2

P: n/a
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.
Feb 19 '08 #3

P: n/a
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.
Feb 19 '08 #4

P: n/a
Nick Stinemates <ni**@stinemates.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
Feb 19 '08 #5

P: n/a
Ben Finney wrote:
Nick Stinemates <ni**@stinemates.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.
Feb 19 '08 #6

This discussion thread is closed

Replies have been disabled for this discussion.