Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
Before you keep reading...
Making great stuff takes time and $$. If you appreciate the book you are reading now and want to keep quality materials free for other students please consider a donation to Runestone Academy. We ask that you consider a $10 donation, but if you can give more thats great, if $10 is too much for your budget we would be happy with whatever you can afford as a show of support.
12.9. Variables and parameters are local¶
An assignment statement in a function creates a local variable for the variable on the left hand side of the
assignment operator. It is called local because this variable only exists inside the function and you cannot use it
outside. For example, consider again the
Try running this in Codelens. When a function is invoked in Codelens, the local scope is separated from global scope by
a blue box. Variables in the local scope will be placed in the blue box while global variables will stay in the global
frame. If you press the ‘last >>’ button you will see an error message. When we try to use
y on line 6 (outside the
function) Python looks for a global variable named
y but does not find one. This results in the error:
Name Error: 'y' is not defined.
y only exists while the function is being executed — we call this its lifetime. When the
execution of the function terminates (returns), the local variables are destroyed. Codelens helps you visualize this
because the local variables disappear after the function returns. Go back and step through the statements paying
particular attention to the variables that are created when the function is called. Note when they are subsequently
destroyed as the function returns.
Formal parameters are also local and act like local variables. For example, the lifetime of
x begins when
square is called, and its lifetime ends when the function completes its execution.
So it is not possible for a function to set some local variable to a value, complete its execution, and then when it is called again next time, recover the local variable. Each call of the function creates new local variables, and their lifetimes expire when the function returns to the caller.
Check Your Understanding
v1 += 1 Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'v1' is not defined def foo(): v1 += 1 foo() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in foo UnboundLocalError: local variable 'v1' referenced before assignment
In the code above, notice and understand the different error messages. The local variables are created at the same time the local namespace is created. That is any variable that is assigned to anywhere in the function gets added to the local namespace immediately but it will remain unbound until the assignment statement is executed.