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

  • Home
  • SEARCH
  • 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 7075355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:10:03+00:00 2026-05-28T06:10:03+00:00

For the following Python 2.7 code: #!/usr/bin/python def func_a(): print "func_a" c = 0

  • 0

For the following Python 2.7 code:

#!/usr/bin/python

def func_a():
   print "func_a"
   c = 0 
   def func_b():
      c += 3
      print "func_b", c

   def func_c():
      print "func_c", c

   print "c", c
   func_b()
   c += 2
   func_c()
   c += 2
   func_b()
   c += 2
   func_c()
   print "end"

func_a()

I get the following error:

File "./a.py", line 9, in func_b
    c += 3
UnboundLocalError: local variable 'c' referenced before assignment

But when I comment out the line c += 3 in func_b, I get the following output:

func_a
c 0
func_b 0
func_c 2
func_b 4
func_c 6
end

Isn’t c being accessed in both cases of += in func_b and = in func_c? Why doesn’t it throw error for one but not for the other?

I don’t have a choice of making c a global variable and then declaring global c in func_b. Anyway, the point is not to get c incremented in func_b but why it’s throwing error for func_b and not for func_c while both are accessing a variable that’s either local or global.

  • 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-05-28T06:10:03+00:00Added an answer on May 28, 2026 at 6:10 am

    What you are seeing here is the difference between accessing and assigning variables. In Python 2.x you can only assign to variables in the innermost scope or the global scope (the latter is done by using the global statement). You can access variables in any enclosing scope, but you cannot access a variable in an enclosing scope and then assign to it in the innermost or global scope.

    What this means is that if there is any assignment to a name inside of a function, that name must already be defined in the innermost scope before the name is accessed (unless the global statement was used). In your code the line c += 3 is essentially equivalent to the following:

    tmp = c
    c = tmp + 3
    

    Because there is an assignment to c in the function, every other occurrence of c in that function will only look in the local scope for funcB. This is why you see the error, you are attempting to access c to get its current value for the +=, but in the local scope c has not been defined yet.

    In Python 3 you could get around this issue by using the nonlocal statement, which allows you to assign to variables that are not in the current scope, but are also not in the global scope.

    Your code would look something like this, with a similar line at the top of funcC:

       def funcB():
          nonlocal c
          c += 3
          ...
    

    In Python 2.x this isn’t an option, and the only way you can change the value of a nonlocal variable is if it is mutable.

    The simplest way to do this is to wrap your value in a list, and then modify and access the first element of that list in every place where you had previously just used the variable name:

    def funcA():
       print "funcA"
       c = [0]
       def funcB():
          c[0] += 3
          print "funcB", c[0]
    
       def funcC():
          c[0] = 5
          print "funcC", c[0]
    
       print "c", c[0]
       funcB()
       funcC()
       funcB()
       funcC()
       print "end"
    
    funcA()
    

    …and the output:

    funcA
    c 0
    funcB 3
    funcC 5
    funcB 8
    funcC 5
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: #!/usr/bin/env python import sys from PyQt4 import QtGui, QtCore
the code here below: #!/usr/bin/env python import wx class MyForm(wx.Frame): def __init__(self): wx.Frame.__init__(self, None,
I have the following Python code: #!/usr/bin/env python2.6 class container(object): name = 'container' configuration
I have opened a shelve using the following code: #!/usr/bin/python import shelve #Module:Shelve is
I have the following Python code: cursor.execute("INSERT INTO table VALUES var1, var2, var3,") where
in the following python code: narg=len(sys.argv) print @length arg= , narg if narg ==
What is the Python equivalent of the following code in Ruby? def loop cont=nil
I have a script a.py : #!/usr/bin/env python def foo(arg1, arg2): return int(arg1) +
I have the following Python code: import xml.dom.minidom import xml.parsers.expat try: domTree = ml.dom.minidom.parse(myXMLFileName)
When running the following python code: >>> f = open(rmyfile.txt, a+) >>> f.seek(-1,2) >>>

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.