Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1101047
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:58:37+00:00 2026-05-17T00:58:37+00:00

I have a nested function where I am trying to access variables assigned in

  • 0

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()
  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:58:37+00:00Added an answer on May 17, 2026 at 12:58 am

    The way Python’s nested scopes work, you can never assign to a variable in the parent scope, unless it’s global (via the global keyword). This changes in Python 3 (with the addition of nonlocal), but with 2.x you’re stuck.

    Instead, you have to sort of work around this by using a datatype which is stored by reference:

    def A():
        foo = [1]
        def B():
            foo[0] = 2 # since foo is a list, modifying it here modifies the referenced list
    

    Note that this is why your list variables work – they’re stored by reference, and thus modifying the list modifies the original referenced list. If you tried to do something like path = [] inside your nested function, it wouldn’t work because that would be actually assigning to path (which Python would interpret as creating a new local variable path inside the nested function that shadows the parent’s path).

    One option that is sometimes used is to just keep all of the things that you want to persist down into the nested scope in a dict:

    def A():
        state = {
            'path': [],
            'current': 0,
            # ...
        }
    
        def B():
            state['current'] = 3
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can we have a nested function in C? What is the use of nested
I have nested iframes and I would like to use onload function for the
How would I create a nested list, I currently have this public function getNav($cat,$subcat){
Anyone have a slick function that will return a nested tree of all the
I have a large nested list that I am trying to animate using jquery
I have a multi-dimensional multi-object array from a simplexml_import_dom() function call. A slice of
'm trying to call a server side function in nested jquery dialogs the design
I have an application with a nested list I am trying to manage with
I'm trying to extend a function I wrote to find the first 3 values
I am trying to use a closure to ensure that a function can only

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.