Getting the module name from a method...

Andrew Csillag andrew at starmedia.net
Tue Aug 17 14:21:47 EDT 1999


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 at geocities.com>




More information about the Python-list mailing list