Skip to main content
added 115 characters in body
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 131
  • 193

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

or in some older versions:

UnboundLocalError: 'c' not assigned

If I comment out c += 1, all the 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 How can I use a global variable 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.

See How can "NameError: free variable 'var' referenced before assignment in enclosing scope" occur in real code? for a related problem caused by the del keyword.

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

or in some older versions:

UnboundLocalError: 'c' not assigned

If I comment out c += 1, all the 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 How can I use a global variable 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.

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

or in some older versions:

UnboundLocalError: 'c' not assigned

If I comment out c += 1, all the 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 How can I use a global variable 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.

See How can "NameError: free variable 'var' referenced before assignment in enclosing scope" occur in real code? for a related problem caused by the del keyword.

Combine disjointed paragraph. Fix typo: "both"; there are three prints.
Source Link
wjandrea
  • 33.8k
  • 10
  • 69
  • 105

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

or in newersome older versions of Python, or:

UnboundLocalError: 'c' not assigned

in some older versions.

If I comment out c += 1, bothall the 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 How can I use a global variable 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.

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 How can I use a global variable 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.

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

or in some older versions:

UnboundLocalError: 'c' not assigned

If I comment out c += 1, all the 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 How can I use a global variable 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.

edited tags
Link
Karl Knechtel
  • 61.4k
  • 14
  • 131
  • 193
expand see-alsos
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 131
  • 193
Loading
put the erroneous code up front; simplify MRE; show modern error message; rm irrelevant details; streamline the explanation; edit title for clarity and to describe necessary and sufficient conditions accurately
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 131
  • 193
Loading
added 8 characters in body; edited tags
Source Link
Super Kai - Kazuya Ito
  • 42.5k
  • 22
  • 256
  • 253
Loading
another crosslink
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 131
  • 193
Loading
improve clarity/precision; switch to code fences; add see-also section
Source Link
Karl Knechtel
  • 61.4k
  • 14
  • 131
  • 193
Loading
Question Protected by TylerH
Now that this seems to be a duplicate target, it should have a usable title.
Link
Carcigenicate
  • 46.4k
  • 12
  • 79
  • 133
Loading
removed greeting and formatting
Source Link
TankorSmash
  • 12.9k
  • 6
  • 70
  • 108
Loading
switched to markdown, fixed typos and removed the disallowed word 'question' in title
Source Link
Loading
tidying
Source Link
Harley Holcombe
  • 183.2k
  • 15
  • 73
  • 63
Loading
added 315 characters in body
Source Link
tba
  • 6.7k
  • 8
  • 45
  • 63
Loading
Source Link
tba
  • 6.7k
  • 8
  • 45
  • 63
Loading