0

As the title suggests, when I try to execute this function:

num = 3


def add_num():
    num += 6
    print(num)


add_num()
print(num)

it gives this error:

Traceback (most recent call last):
  File "C:/Users/Sean/Desktop/Programming/Python_prac/test.py", line 9, in <module>
    add_num()
  File "C:/Users/Sean/Desktop/Programming/Python_prac/test.py", line 5, in add_num
    num += 6
UnboundLocalError: local variable 'num' referenced before assignment

What confuses me is that you can reference "num" within the function once you don't try to assign it to itself. However, wouldn't the "num" variable created in the function just point to another location in memory and this not be an issue?

Could someone explain this behavior.

sean harricharan
  • 43
  • 1
  • 2
  • 7
  • Sounds like a misunderstanding of [how Python resolves namespaces and scope](https://realpython.com/python-scope-legb-rule/) – Joshua Voskamp Nov 10 '21 at 15:33
  • Does this answer your question? [Why does writing to a variable change its scope?](https://stackoverflow.com/questions/64801735/why-does-writing-to-a-variable-change-its-scope) – MisterMiyagi Nov 10 '21 at 16:13

4 Answers4

2

If you try to reference a global variable or call any of its methods from within a function then it will work fine. But the problem comes when trying to change one - the += operator doesn't actually change the value stored in memory because ints are immutable. Therefore, it creates a new variable, also with the name num, equal to the previous value plus 6. But when Python sees any operator to create a new variable of a certain name within a function, it immediately assumes you are trying to reference the local variable as opposed to the global one. To fix this, use the global keyword:

num = 3

def add_num():
    global num
    num += 6
    print(num)

add_num()
print(num)

What's even more odd is the fact that even if the line creating a new variable with name num were to be right at the bottom of the function, trying to reference num at the top of the function would still give a syntax error because Python sees the = operator at the bottom of the function even when still at the top of the function:

def add_num():
    print(num)  # Gives a syntax error
    num = 5
Lecdi
  • 2,189
  • 2
  • 6
  • 20
  • I'm familiar with scopes in python and know how it works. I know how to solve this problem but was looking for some documentation on it because This behavior is kind of tricky to grasp fully. – sean harricharan Nov 10 '21 at 20:12
0
num = 3


def add_num():
    global num
    num += 6
    print(num)


add_num()
print(num)

will do

Shanavas M
  • 1,581
  • 1
  • 17
  • 24
  • 2
    You were the first to post this answer, three more people posted it after you, yet it's yours that's in the low-quality answer review queue. I voted to keep it, but maybe the answers explaining the `global` keyword will end up being preferred. In general, it's best to put in some text explaining the problem and the solution rather than just post code. – joanis Nov 10 '21 at 20:47
0

That's because you num variable scope is local to the function. In order to be able to access the global num variable, you have to leverage the global keyword:

um = 3


def add_num():
    global num
    num += 6
    print(num)


add_num()
print(num)

Output:

9
CanciuCostin
  • 1,773
  • 1
  • 10
  • 25
0

To use the variable that is not inside the scope of a function, we can use global keyword to use it.

num = 3

def add_num():
    global num
    num += 6
    print(num)

add_num()
print(num)
Robert
  • 7,394
  • 40
  • 45
  • 64