See what Python module instantised a class?
<p>Is it possible in a Python class to see the name (or other identifier) of the class or module that instantises it?</p>

<p>For example:</p>

<pre><code># mymodule.py

class MyClass():
    def __init__(self):
        print(instantised_by)

#main.py

from mymodule import MyClass

instance = MyClass()
</code></pre>

<p>Running <code>main.py</code> should print:</p>

<pre><code>main
</code></pre>

<p>or something like that.</p>

<p>Is this possible?</p>

<p>With <code>traceback</code> module:</p>

<pre><code>import traceback

class MyClass():
    def __init__(self):
       file,line,w1,w2 = traceback.extract_stack()[1]
       print(w1)
</code></pre>

python