Possible to pass a function into a object's method as an argument, which can refer to that object?
<pre><code>def my_func():
    stuff
    return something

my_object.my_method(my_func)
</code></pre>

<p>I want my_func to be able to refer to <code>my_object</code>. Essentially like deferred <code>self</code>, which means "whatever object I am in, refer to that"</p>

<p>You could use the <strong><a href="https://docs.python.org/2/library/inspect.html" rel="nofollow"><code>inspect</code></a></strong> module</p>

<pre><code>import inspect

def my_func():
    stuff
    my_object = inspect.stack()[1][0].f_locals["self"]
    return something
</code></pre>

<p>However, I guess a simpler approach would be to pass the instance as argument, such as </p>

<pre><code>def my_func():
    stuff
    return something

my_object.my_method(my_func, caller=my_object)
</code></pre>

python python-2.7 functional-programming self function-object