This is the official explanation you're looking for
>>> x = 10 >>>
def foo():
...print(x)
...x += 1 >>>
foo()
Traceback(most recent call last):
...
UnboundLocalError: local variable 'x'
referenced before assignment
In third, c
is assigned inside the bloc, so it is local. But it is used before assignement, hence the error. If you want to use the global value, you must declare it as global:
def fib():
global c
a = c
print("a in fib", a)
print("c in fib", c)
c = 20
Python "assumes" that we want a local variable due to the assignment to a inside of fib(), so the first print statement throws this error message. Any variable which is changed or created inside of a function is local, if it hasn't been declared as a global variable. To tell Python, that we want to use the global variable, we have to use the keyword "global", as can be seen in the following example:
c = 100
def fib():
global c
a = c
print("a in fib", a)
print("c in fib", c)
c = 20
fib()
print("c", c)
When we use global variables we fundamentally make it available to all the functions that we have defined or plan to define. In your code when you say
c=100
You have a global variable defined. But to use it inside a function without being hidden by a local variable, you have do this
def fib():
global c
print(c)
c = 10
a = c
print(a, c)
By Bernd Klein. Last modified: 29 Jun 2022.
def f():
print(s)
s = "I love Paris in the summer!"
f()
I love Paris in the summer!
def f():
s = "I love London!"
print(s)
s = "I love Paris!"
f()
print(s)
I love London!
I love Paris!
def f():
print(s)
s = "I love London!"
print(s)
s = "I love Paris!"
f()
---------------------------------------------------------------------------
UnboundLocalError Traceback (most recent call last)
<ipython-input-3-d7a23bc83c27> in <module>
5
6 s = "I love Paris!"
----> 7 f()
<ipython-input-3-d7a23bc83c27> in f()
1 def f():
----> 2 print(s)
3 s = "I love London!"
4 print(s)
5
UnboundLocalError: local variable 's' referenced before assignment
Last Updated : 22 Jun, 2022
Output:
25
global means “a name at the top-level of a module file.”,Each module is a global scope—a namespace where variables created (assigned) at the top level of a module file live.,When outside a function (i.e., at the top-level of a module or at the interactive prompt), the local scope is the same as the global—a module’s namespace.,“Global” declarations map assigned names to an enclosing module’s scope.
In Python, variables that are only referenced inside a function are implicitly global. If a variable is assigned a value anywhere within the function’s body, it’s assumed to be a local unless explicitly declared as global.,This explicit declaration is required in order to remind you that (unlike the superficially analogous situation with class and instance variables) you are actually modifying the value of the variable in the outer scope:,Guido van Rossum recommends avoiding all uses of from <module> import ..., and placing all code inside functions. Initializations of global variables and class variables should use constants or built-in functions only. This means everything from an imported module is referenced as <module>.<name>.,Because of this feature, it is good programming practice to not use mutable objects as default values. Instead, use None as the default value and inside the function, check if the parameter is None and create a new list/dictionary/whatever if it is. For example, don’t write:
>>> x = 10 >>> def bar(): ...print(x) >>> bar() 10
>>> x = 10 >>> def foo(): ...print(x) ...x += 1
>>> foo()
Traceback(most recent call last):
...
UnboundLocalError: local variable 'x'
referenced before assignment
>>> x = 10 >>>
def foobar():
...global x
...print(x)
...x += 1 >>>
foobar()
10
>>> print(x) 11
>>> def foo():
...x = 10
...def bar():
...nonlocal x
...print(x)
...x += 1
...bar()
...print(x) >>>
foo()
10
11