Getting the module name from a method...
Michael P. Reilly
arcege at shore.net
Tue Aug 17 11:27:45 EDT 1999
Olivier Deckmyn <olivier.deckmyn at mail.dotcom.fr> wrote:
: Hi !
: I would like a method of my own to be able to print the module it is
: declared in:
: the text for the module : toto.py is :
: class MyClass:
: def myMethod(self):
: print "I am in module", ????????
: and then
: should produce :
Unless the method was added as an instance attribute (self.method = ...),
the global namespace should be the module where the method was defined,
in this case "toto". The less secure means would be
globals()['__name__'], but you could also go with:
import os, sys
exc, val, tb = sys.exc_info()
code = tb.tb_frame.f_back.f_code
del exc, val, tb
module = os.path.splitext(os.path.basename(code.co_filename))
module = '<string>'
More information about the Python-list