Steve Holden wrote:
> Neal Becker wrote:
> >
> > Still curious about the answer.  If I know that I am imported from __main__,
> > then I can do access X as sys.modules[__main__].X.  In general, I don't
> > know how to determine who is importing me.
> >
> I don't think you can without huge amounts of introspection - it's even
> worse than the "what's the name of this object" question that seems to
> come up regularly.

import sys

frame = sys._getframe()
caller = frame.f_back
print 'Called from', caller.f_code.co_filename, caller.f_lineno
# for more info, look into the traceback module

> A module can be imported from multiple modules, and
> you only get to execute code on the first import.
> Even then (on the first import) I am not sure how you could introspect
> to find the answer you want.

You can install your own __import__() hook to catch all imports.

Just because you can do something, it doesn't follow that you should do
it, especially in this case.  Unless you really, really need these
tricks, they shouldn't be used.

n

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to