How can I know from where the method is called?
<p>I'm trying clean implement of Objective-C's category in Python, and found <a href="http://stackoverflow.com/a/13568709/2100702">this answer</a> to similar question of mine. I copied the code below:</p>

<p><strong>categories.py</strong></p>

<pre><code>class category(object):
    def __init__(self, mainModule, override = True):
        self.mainModule = mainModule
        self.override = override

    def __call__(self, function):
        if self.override or function.__name__ not in dir(self.mainModule):
            setattr(self.mainModule, function.__name__, function)
</code></pre>

<p>But I do not want to waste namespace.
By using this `categories', there remains a variable as NoneType object like below:</p>

<pre><code>&gt;&gt;&gt; from categories import category
&gt;&gt;&gt; class Test(object):
...     pass
... 
&gt;&gt;&gt; @category(Test)
... def foobar(self, msg):
...     print msg
... 
&gt;&gt;&gt; test = Test()
&gt;&gt;&gt; test.foobar('hello world')
hello world
&gt;&gt;&gt; type(foobar)
&lt;type 'NoneType'&gt;
&gt;&gt;&gt; 
</code></pre>

<p>I want it to be like below</p>

<pre><code>&gt;&gt;&gt; from categories import category
&gt;&gt;&gt; class Test(object):
...     pass
... 
&gt;&gt;&gt; @category(Test)
... def foobar(self, msg):
...     print msg
... 
&gt;&gt;&gt; test = Test()
&gt;&gt;&gt; test.foobar('hello world')
hello world
&gt;&gt;&gt; type(foobar)
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
NameError: name 'foobar' is not defined
&gt;&gt;&gt; 
</code></pre>

<p>Is there anyway to delete it automatically like below?</p>

<pre><code>    def __call__(self, function):
        if self.override or function.__name__ not in dir(self.mainModule):
            setattr(self.mainModule, function.__name__, function)
            del(somewhere.function.__name__)
</code></pre>

<p>I found that <code>sys._getframe</code> give me some useful information. But I couldn't make it by myself.</p>
