Local Variables

suggest change

If a name is bound inside a function, it is by default accessible only within the function:

def foo():
    a = 5
    print(a) # ok

print(a) #  NameError: name 'a' is not defined

Control flow constructs have no impact on the scope (with the exception of except), but accessing variable that was not assigned yet is an error:

def foo():
    if True: 
        a = 5
    print(a) # ok

b = 3
def bar():
    if False:
        b = 5
    print(b) # UnboundLocalError: local variable 'b' referenced before assignment

Common binding operations are assignments, for loops, and augmented assignments such as a += 5

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents