Mailing List Archive

Getting the module name from a method...
Hi !

I would like a method of my own to be able to print the module it is
declared in:

ex:
the text for the module : toto.py is :

class MyClass:
def myMethod(self):
print "I am in module", ????????

and then
MyClass().myMethod()
should produce :
'toto'

....
Thanx for your help !

---
Olivier Deckmyn, Paris - France
"Any sufficiently advanced technology is indistinguishable from
magic." -Arthur C. Clark
Getting the module name from a method... [ In reply to ]
Olivier Deckmyn <olivier.deckmyn@mail.dotcom.fr> wrote:
: Hi !

: I would like a method of my own to be able to print the module it is
: declared in:

: ex:
: the text for the module : toto.py is :

: class MyClass:
: def myMethod(self):
: print "I am in module", ????????

: and then
: MyClass().myMethod()
: should produce :
: 'toto'

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
def mymodule():
try:
raise RuntimeError
except RuntimeError:
exc, val, tb = sys.exc_info()
code = tb.tb_frame.f_back.f_code
del exc, val, tb
if code.co_filename:
module = os.path.splitext(os.path.basename(code.co_filename))[0]
else:
module = '<string>'
return module

-Arcege
Getting the module name from a method... [ In reply to ]
Olivier Deckmyn wrote:
> I would like a method of my own to be able to print the module it is
> declared in:
>
> ex:
> the text for the module : toto.py is :
>
> class MyClass:
> def myMethod(self):
> print "I am in module", ????????
>
> and then
> MyClass().myMethod()
> should produce :
> 'toto'

How about "self.__class__.__module__"?

Regards,
Stephen

--
Stephen J. Turner <sjturner@ix.netcom.com>
Getting the module name from a method... [ In reply to ]
Olivier Deckmyn wrote:
>
> Hi !
>
> I would like a method of my own to be able to print the module it is
> declared in:
>
> ex:
> the text for the module : toto.py is :
>
> class MyClass:
> def myMethod(self):
> print "I am in module", ????????
>
> and then
> MyClass().myMethod()
> should produce :
> 'toto'
>
> ....
> Thanx for your help !
>
> ---
> Olivier Deckmyn, Paris - France
> "Any sufficiently advanced technology is indistinguishable from
> magic." -Arthur C. Clark
the name of the current module is __name__
so if you have a module foo.py
def f():
print __name__

and the main executable:
import foo
foo.f()

will print 'foo'

--
"Programmers are just machines that convert coffee to executable code"
-Unknown

"Drew Csillag" <drew_csillag@geocities.com>
Getting the module name from a method... [ In reply to ]
"Stephen J. Turner" wrote:
>
> How about "self.__class__.__module__"?

No, that will give you the module of the
instance's class. This is not necessarily
the same as the class defining the currently
running method, which could be an inherited
class.

Greg