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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:05:53+00:00 2026-05-10T23:05:53+00:00

When I try this code: a, b, c = (1, 2, 3) def test():

  • 0

When I try this code:

a, b, c = (1, 2, 3)  def test():     print(a)     print(b)     print(c)     c += 1 test() 

I get an error from the print(c) line that says:

UnboundLocalError: local variable 'c' referenced before assignment 

in newer versions of Python, or

UnboundLocalError: 'c' not assigned 

in some older versions.

If I comment out c += 1, both prints are successful.

I don’t understand: why does printing a and b work, if c does not? How did c += 1 cause print(c) to fail, even when it comes later in the code?

It seems like the assignment c += 1 creates a local variable c, which takes precedence over the global c. But how can a variable "steal" scope before it exists? Why is c apparently local here?


See also Using global variables in a function for questions that are simply about how to reassign a global variable from within a function, and Is it possible to modify a variable in python that is in an outer (enclosing), but not global, scope? for reassigning from an enclosing function (closure).

See Why isn't the 'global' keyword needed to access a global variable? for cases where OP expected an error but didn’t get one, from simply accessing a global without the global keyword.

See How can a name be "unbound" in Python? What code can cause an `UnboundLocalError`? for cases where OP expected the variable to be local, but has a logical error that prevents assignment in every case.

  • 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. 2026-05-10T23:05:54+00:00Added an answer on May 10, 2026 at 11:05 pm

    Python treats variables in functions differently depending on whether you assign values to them from inside or outside the function. If a variable is assigned within a function, it is treated by default as a local variable. Therefore, when you uncomment the line, you are trying to reference the local variable c before any value has been assigned to it.

    If you want the variable c to refer to the global c = 3 assigned before the function, put

    global c 

    as the first line of the function.

    As for python 3, there is now

    nonlocal c 

    that you can use to refer to the nearest enclosing function scope that has a c variable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 87k
  • Answers 87k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It seemed like cleaning the solution, closing Visual Studio, deleting… May 11, 2026 at 5:32 pm
  • Editorial Team
    Editorial Team added an answer Maybe try: for cat in cats: for item in ['apple',… May 11, 2026 at 5:32 pm
  • Editorial Team
    Editorial Team added an answer I think there's not. Although, you can do one of… May 11, 2026 at 5:32 pm

Related Questions

In my C++ header files I try to use forward declarations (class MyClass;) instead
I'm writing a global error handling module for one of my applications. One of
When I try this code: class MyStuff: def average(a, b, c): # Get the
Possible Duplicate: How to properly clean up Excel interop objects in C# I've read

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.