When you call a function the computer creates a fresh list of variables

But when you call a function, Python starts to record any new variables created in the function's code on a new sheet of paper on the stack:

Mew statk iv-ame def set_password():

passwords"C8H10N402"

This new sheet of paper on the stack is called a new stack frame. Stack frames record all of the new variables that are created within a function. These are known as local variables.

The variables that were created before the function was called are still there if the function needs them; they are on the previous stack frame.

But why does the computer record variables like this?

Your program creates a new stack frame each time it calls a function, allowing the function to have its own separate set of variables. If the function creates a new variable for some internal calculation, it does so on its own stack frame without affecting the already existing variables in the rest of the program.

This mechanism helps keep things organized, but it has a side-effect that is causing problems...

Mew statk iv-ame def set_password():

passwords"C8H10N402"

The calling code's variables are still here.

LOCALfiable* used by the Wio*

The calling code's variables are still here.

When a variable's value can be seen by some code, it is said to be "in scope."

local garbage removal

Was this article helpful?

0 0

Post a comment