python - calling locals() in a function nested in a class method
<p>Here is a snippet:</p>

<pre><code>class Foo(object):
    def bar(self, x):
        def baz(y):
            print locals()['self']
        ..
        return baz(..)

foo = Foo()
foo.bar(..)
</code></pre>

<p>I have 2 boxes. On Windows Vista with Python 2.5 calling <code>foo.bar(..)</code> works. On Ubuntu with Python 2.7 calling <code>foo.bar(..)</code> throws a <code>KeyError</code>, not being able to find <code>self</code> in locals() dict.</p>

<p>Any ideas?</p>

<p><strong>Edit: I owe an apology; it seems I have misguided you while trying to frame the problem.</strong> The actual code in the nested function evaluates a string coming from a DSL:</p>

<pre><code>            r = re.compile(r'\$(\w+)')
            eval_string = r.sub(r'self.player.stats["\1"]', s)
            result = eval(eval_string)
</code></pre>

<p>It runs Vista/Python 2.5, fails on Ubuntu/Python 2.7.</p>
