How to find out in which module an instance of a class is created?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Thu Aug 13 02:16:15 EDT 2009


En Mon, 10 Aug 2009 18:51:59 -0300, Johannes Janssen  
<mail at johannes-janssen.de> escribió:

> class A(object):
>     def __init__(self, mod=None):
>         if mod is None:
>             self.mod = sys._getframe(1).f_globals['__name__']
>         else:
>             self.mod = mod
>
> In warnings.warn() they used try around sys._getframe(1). As far as I  
> understand what is done in warnings, there it is not sure what object  
> caused the warning and therefore it is not sure whether you can or  
> cannot use sys._getframe(1). Though in my case it should be quite clear.  
> Can I be sure that my code will always work?

The try/except around sys._getframe(1) is because that function is not  
mandatory/available on all Python implementations (that's the case for  
jython which doesn't provide it).

-- 
Gabriel Genellina




More information about the Python-list mailing list