-->

Local and Global Scope [Python]

Parameters and variables that are assigned in a called function are said to exist in that function’s local scope. Variables that are assigned outside all functions are said to exist in the global scope. A variable that exists in a local scope is called a local variable, while a variable that exists in the global scope is called a global variable. A variable must be one or the other; it cannot be both local and global.



Think of a scope as a container for variables. When a scope is destroyed, all the values stored in the scope’s variables are forgotten. There is only one global scope, and it is created when your program begins. When your program terminates, the global scope is destroyed, and all its variables are forgotten.

Otherwise, the next time you ran your program, the variables would remember their values from the last time you ran it. A local scope is created whenever a function is called. Any variables assigned in this function exist within the local scope. When the function returns, the local scope is destroyed, and these variables are forgotten. The next time you call this function, the local variables will not remember the values stored in them from the last time the function was called. 

Baca Juga

Scopes matter for several reasons:
  • Code in the global scope cannot use any local variables.
  • However, a local scope can access global variables.
  • Code in a function’s local scope cannot use variables in any other local scope.
  • You can use the same name for different variables if they are in different scopes. That is, there can be a local variable named spam and a global variable also named spam.

The reason Python has different scopes instead of just making everything a global variable is so that when variables are modified by the code in a particular call to a function, the function interacts with the rest of the program only through its parameters and the return value. This narrows down the list code lines that may be causing a bug. If your program contained nothing but global variables and had a bug because of a variable being set to a bad value, then it would be hard to track down where this bad value was set. It could have been set from anywhere in the program—and your program could be hundreds or thousands of lines long! But if the bug is because of a local variable with a bad value, you know that only the code in that one function could have set it incorrectly.

While using global variables in small programs is fine, it is a bad habit to rely on global variables as your programs get larger and larger. Local Variables Cannot Be Used in the Global Scope Consider this program, which will cause an error when you run it:


  1. def spam():
  2. eggs = 31337
  3. spam()
  4. print(eggs)


If you run this program, the output will look like this:

  1. Traceback (most recent call last):
  2. File "C:/test3784.py", line 4, in <module>
  3. print(eggs)
  4. NameError: name 'eggs' is not defined


The error happens because the eggs variable exists only in the local scope created when spam() is called. Once the program execution returns from spam, that local scope is destroyed, and there is no longer a variable named eggs. So when your program tries to run print(eggs), Python gives you an error saying that eggs is not defined. This makes sense if you think about it; when the program execution is in the global scope, no local scopes exist, so there can’t be any local variables. This is why only global variables can be used in the global scope.

Local Scopes Cannot Use Variables in Other Local Scopes A new local scope is created whenever a function is called, including when a function is called from another function. Consider this program:

  1. def spam():
  2. eggs = 99
  3. bacon()
  4. print(eggs)
  5. def bacon():
  6. ham = 101
  7. eggs = 0
  8. spam()

When the program starts, the spam() function is called y, and a local scope is created. The local variable eggs u is set to 99. Then the bacon() function is called v, and a second local scope is created. Multiple local scopes can exist at the same time. In this new local scope, the local variable ham is set to 101, and a local variable eggs—which is different from the one in spam()’s local scope—is also created x and set to 0.
When bacon() returns, the local scope for that call is destroyed. The program execution continues in the spam() function to print the value of eggs w, and since the local scope for the call to spam() still exists here, the eggs variable is set to 99. This is what the program prints.
The upshot is that local variables in one function are completely separate from the local variables in another function.

Related Posts

0 Response to "Local and Global Scope [Python]"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel