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 7990043
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T12:54:00+00:00 2026-06-04T12:54:00+00:00

I don’t quite understand what is going with variable ‘current_label’, which to me seems

  • 0

I don’t quite understand what is going with variable ‘current_label’, which to me seems to be defined in the enclosing code of function ‘ts_r’, which would make it visible from inside ‘ts_r’? But when I run the code below, it complains that local variable ‘current_label’ is referenced before assignment… Note that it does not complain about ‘visited’ or ‘f’, and that it won’t complain if I initialize ‘current_label’ with [len(g)].

def topological_sort(g):

    visited = zeros((len(g)), dtype='int32')
    f = zeros((len(g)), dtype='int32')
    current_label = len(g) # [] so it is seen inside ts_r

    def ts_r(n):
        for nn in [v for v in g[n] if not visited[v]]:
            visited[nn] = 1
            ts_r(nn)
        f[n] = current_label
        current_label -= 1

    for i in range(len(g)):
        if not visited[i]: 
            ts_r(i)

    return f
  • 1 1 Answer
  • 0 Views
  • 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-06-04T12:54:02+00:00Added an answer on June 4, 2026 at 12:54 pm

    In case of visited or f you change mutable variables. In case of current_label you try to re-assign value to global variable without stating it is global.

    Changing variables from outside scopes does not require declaring them global, but reassigning value to a global variable requires declaration, that it is global – otherwise it is treated as local (and in case of referencing before assignement you get such errors).

    Lets look at the code:

    1.    def ts_r(n):
    2.        for nn in [v for v in g[n] if not visited[v]]:
    3.            visited[nn] = 1
    4.            ts_r(nn)
    5.        f[n] = current_label
    6.        current_label -= 1
    

    In line 5 you assign global variable value to f[n], but later, in line 6 you try to assign this global variable a value. You did not tell Python it is global, thus it assumes it is local. But if it is local, you cannnot assign it earlier.

    You have two choices:

    1. (probably not the one you are looking for) Use it as local:

      def ts_r(n):
          current_label = len(g)  # initialize local variable
          for nn in [v for v in g[n] if not visited[v]]:
              visited[nn] = 1
              ts_r(nn)
          f[n] = current_label
          current_label -= 1
      
    2. Tell Python it is global variable and you would like to change global variable’s value:

      def ts_r(n):
          global current_label  # current_label is now global
          for nn in [v for v in g[n] if not visited[v]]:
              visited[nn] = 1
              ts_r(nn)
          f[n] = current_label
          current_label -= 1
      

    EDIT:

    After your question was updated, I saw nested functions instead of functions defined in global scope. Thus the solution with global won’t work.

    In Python 3.x you have nonlocal keyword, but you will need to find walkaround in case of Python 2.x. Again, you have at least two possibilities:

    1. Use mutable variable enclosing immutable you want to change (eg. list with one integer in it). Then when you just refer to (and change) the first element of a list. Try it.

    2. Another solution is to add an attribute for the wrapping function (function is also a mutable, so you can change it, but you will not pollute global namespace). Example is here: http://ideone.com/7jGvM. In your case it may look like this:

      def topological_sort(g):
      
          visited = zeros((len(g)), dtype='int32')
          f = zeros((len(g)), dtype='int32')
          topological_sort.current_label = len(g) # [] so it is seen inside ts_r
      
          def ts_r(n):
              for nn in [v for v in g[n] if not visited[v]]:
                  visited[nn] = 1
                  ts_r(nn)
              f[n] = topological_sort.current_label
              topological_sort.current_label -= 1
      
          for i in range(len(g)):
              if not visited[i]: 
                  ts_r(i)
      
          return f
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Don't quite understand determinism in the context of concurrency and parallelism in Haskell. Some
Don't quite understand why this copy constructor is not invoked when I build with
Don't be scared of the extensive code. The problem is general. I just provided
I don't see the message Singleton Class when i run the following code. Why
Don't think that I'm mad, I understand how php works! That being said. I
Don't be frightened, its a very basic code. Just wanted to check with you
Don't know whats exactly going on, but it's definitely killing my time for nothing.
Don't they both have to convert to machine code at some point to execute
Don't worry, I'm not going to ask that question, yet again... I am wanting
don't understand: in my controller: @json = User.all.to_gmaps4rails do |user| \Title\: \#{user.email}\ end in

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.