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

Christian Heimes lists at cheimes.de
Mon Aug 10 01:47:34 CEST 2009


Johannes Janssen wrote:
>  > class A(object):
>  >     def __init__(self, mod=__name__):
>  >         self.mod = mod
> 
> .... won't work. In this case mod would always be "foo".

You have to inspect the stack in order to get the module of the caller. 
The implementation of warnings.warn() gives you some examples how to get 
the module name.

Christian



More information about the Python-list mailing list