name of client module
Jeff Schwab
jeff at schwabcenter.com
Mon Feb 18 21:34:40 EST 2008
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.
More information about the Python-list
mailing list