Retreiving/Printing execution context
<p>EDIT: This question has been solved with help from apphacker and ConcernedOfTunbridgeWells. I have updated the code to reflect the solution I will be using.</p>

<p>I am currently writing a swarm intelligence simulator and looking to give the user an easy way to debug their algorithms. Among other outputs, I feel it would be beneficial to give the user a printout of the execution context at the beginning of each step in the algorithm.</p>

<p>The following code achieves what I was needing.</p>

<pre><code>import inspect

def print_current_execution_context():
    frame=inspect.currentframe().f_back #get caller frame
    print frame.f_locals #print locals of caller

class TheClass(object):
    def __init__(self,val):
        self.val=val
    def thefunction(self,a,b):
        c=a+b
        print_current_execution_context()


C=TheClass(2)
C.thefunction(1,2)
</code></pre>

<p>This gives the expected output of:</p>

<pre><code>{'a': 1, 'c': 3, 'b': 2, 'self': &lt;__main__.TheClass object at 0xb7d2214c&gt;}
</code></pre>

<p>Thank you to apphacker and ConcernedOfTunbridgeWells who pointed me towards this answer</p>
