Determining __name__ from the code that called a function

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jul 28 09:40:03 CEST 2009


En Tue, 28 Jul 2009 03:21:10 -0300, Paul Johnston <paul.paj at gmail.com>  
escribió:

> In ToscaWidgets 2 experimental, when defining resources you often do
> something like this:
> CSSLink(modname=__name__, filename='static/mycss.css')
>
> Now, what I'd like to do is make the "modname=__name__" optional, to
> make code more concise. I figure there must be some way (using inspect
> or something) to determine what __name__ would be in the code that
> just called my function. Couldn't immediately see how to do this - any
> suggestions?

In CPython, you may use sys._getframe(1).f_globals['__name__'] but I don't  
know how portable is that across Python implementations.

-- 
Gabriel Genellina




More information about the Python-list mailing list