I have a nested function where I am trying to access variables assigned in the parent scope. From the first line of the next()
function I can see that path
, and nodes_done
are assigned as expected. distance
, current
, and probability_left
have no value and are causing a NameError
to be thrown.
What am I doing wrong here? How can I access and modify the values of current
, distance
, and probability_left
from the next()
function?
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 -> %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()