name of client module

J

Jeff Schwab

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?
 
N

Nick Stinemates

Jeff said:
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 ([email protected])
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: (e-mail address removed)
Yahoo: (e-mail address removed)
==================
 
J

Jeff Schwab

Nick said:
Why would you ever want to do this?
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.
 
N

Nick Stinemates

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.

You're still not explaining a real example of what this could be used for.

Oh well, here's an example of an implementation of what you want to do.

Import.py
================
#!/usr/bin/python

class Importer:
def __init__(self):
pass
def __import__(self, module):
exec "import %s" % module
exec "a = %s" % module
a.setImported(self)

i = Importer()
i.__import__("Imported")
================

Imported.py
================
#!/usr/bin/python

def setImported(importer):
print "I've been imported by %s" %importer
================


--
==================
Nick Stinemates ([email protected])
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: (e-mail address removed)
Yahoo: (e-mail address removed)
==================
 
J

Jeff Schwab

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.
 
N

Nick Stinemates

Jeff said:
Please don't snip so much.



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.



Thanks.
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.

I suppose I did get a bit carried away. It seems people always want to
know 'who is calling my object' but I think writing a design around that is:

a) Really hard to follow, much like GoTo statements
b) Poorly thought out
and, probably much more importantly
c) Grouping code together like that really limits what you can do
with it..

I'm sorry if I offended.

--
==================
Nick Stinemates ([email protected])
http://nick.stinemates.org

AIM: Nick Stinemates
MSN: (e-mail address removed)
Yahoo: (e-mail address removed)
==================
 
J

Jeff Schwab

Nick said:
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.

I suppose I did get a bit carried away. It seems people always want to
know 'who is calling my object' but I think writing a design around that is:

a) Really hard to follow, much like GoTo statements
b) Poorly thought out
and, probably much more importantly
c) Grouping code together like that really limits what you can do
with it..

I'm sorry if I offended.

No problem! In general, I agree with those points completely.
 
B

Ben Finney

Nick Stinemates said:
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 :)
 
J

Jeff Schwab

Ben said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top