filename of calling function?

Gerard Flanagan grflanagan at gmail.com
Sat Nov 28 15:14:53 EST 2009


Phlip wrote:
> Consider these two python modules:
> 
> aa.py
> 
> def a():
>     print '?'
> 
> bb.py
>   import aa
> 
> def bb():
>   aa.a()
> 
> bb()
> 
> How do I make the print line emit the filename of bb.py? (It could be
> anything.)
> 

Possibly not very reliable in every situation (doctests, other pythons, 
...) but this is what I do:


---------  aa.py --------------
import __main__ as CALLER

def mynameis():
     print CALLER.__file__

---------  bb.py --------------

import aa

def whosthere():
     aa.mynameis()

whosthere()

-------------------------------

OUTPUT: bb.py


hth

G.F.




More information about the Python-list mailing list