Numeric variable scope in Python closure (Python v2.5.2)
<p>I have a nested function where I am trying to access variables assigned in the parent scope.  From the first line of the <code>next()</code> function I can see that <code>path</code>, and <code>nodes_done</code> are assigned as expected. <code>distance</code>, <code>current</code>, and <code>probability_left</code> have no value and are causing a <code>NameError</code> to be thrown.</p>

<p>What am I doing wrong here?  How can I access and modify the values of <code>current</code>, <code>distance</code>, and <code>probability_left</code> from the <code>next()</code> function?</p>

<pre><code>def cheapest_path(self):
    path = []
    current = 0
    distance = 0
    nodes_done = [False for _ in range(len(self.graph.nodes))]
    probability_left = sum(self.graph.probabilities)

    def next(dest):
        log('next: %s -&gt; %s distance(%.2f), nodes_done(%s), probability_left(%.2f)' % (distance,self.graph.nodes[current],self.graph.nodes[dest],str(nodes_done),probability_left))
        path.append((current, distance, nodes_done, probability_left))

        probability_left -= self.graph.probabilities[current]
        nodes_done[current] = True
        distance = self.graph.shortest_path[current][dest]
        current = dest

    def back():
        current,nodes_done,probability_left = path.pop()
</code></pre>
