First page Back Continue Last page Overview Graphics

Global and Local

def f():

print(s)

s = "Perl"

print(s)

s = "Python"

f()

print(s)

If we execute the previous script, we get the error message:

UnboundLocalError: local variable 's' referenced before assignment

The variable s is ambigious in f(), i.e. in the first print in f() the global s could be used with the value "Python". After this we define a local variable s with the assignment

s = "Perl"