SoFunction
Updated on 2024-12-19

Understanding and Differences between Global and Local Variables in Python

preamble

Anyone who has studied programming should be familiar with the terms global and local variables, and Python, like most programming languages, also has the concept of global and local variables

But it's different from other programming languages

The essential difference between a global variable and a local variable is the scope.

In layman's terms.

Global variables are declared throughout the py file and are accessible globally

A local variable is declared in a function, and it can only be called from within that function; if you try to call it out of scope, the program blows up!

Defining local variables inside a function with the same name as some global variable can lead to unintended effects that may not be what you expect. Therefore it is not recommended to use it in this way, which makes the program very unsound

Let's look at a few examples directly to understand the difference between global and local variables:

Demo1:

def fun(x):
 y=2
 print("The result of the operation of multiplication:",x*y)
num1=1
print("starting (point)num1=",num1)
fun(num1)
print("The value of y is:",y)

Run results:

The reason for the error is that an attempt was made to access a local variable, but the access was not in the scope of the variable y

Demo2:

def fun():
 num1=2
 print("After modification within the functionnum1=",num1)
num1=1
print("starting (point)num1=",num1)
fun()
print("After running the functionnum1=",num1)

Running results:

You can see that after a modification to a global variable inside a function, the result of the modification is invalidated when the function finishes executing, and the global variable is not affected

Look again:

Demo3:

def fun():
 num1*=2
 print("After modification within the functionnum1=",num1)
num1=1
print("starting (point)num1=",num1)
fun()
print("After running the functionnum1=",num1)

Run results:

This is because the fun() function uses the local variable num1. This is because the fun() function uses the local variable num1, which is just a local variable with the same name as the global variable, and still needs to be assigned a value before it can be used, so again, don't use it this way!

global keyword

If you really want to change the value of a global variable inside a function, you have to use the global keyword

Demo4:

def fun():
 global num1
 num1=2
 print("After modification within the functionnum1=",num1)
num1=1
print("starting (point)num1=",num1)
fun()
print("After running the functionnum1=",num1)

Run results:

Using the global keyword tells the python compiler that the variable is not local but global, which is kind of like a "reference".

nonlocal keyword

Let's look at another variable-related keyword, nonlocal, which literally means that the current variable is not local. nonlocal is a new keyword in Python 3.0, and is not supported.

Let's start with the following code

def fun():
 num2=3
 def fun2():
 num2*=2
 print("num2=",num2)
 return fun2()
 
fun()

Running results:

The reason for the error is similar to the previous one, which is that an undefined local variable is used. However, num2 is not a global variable, it is just an outer variable of the fun2 function, so if you force global to define num2, you will also get an error (try it if you don't believe me).

This is where the nonlocal keyword needs to be used:

def fun():
 num2=3
 def fun2():
 nonlocal num2
 num2*=2
 print("num2=",num2)
 return fun2()
 
fun()

Run results:

In this way, the program can be executed normally

summarize

This article on Python global variables and local variables in the article is introduced to this, more related to Python global variables and local variables content, please search for my previous articles or continue to browse the following related articles I hope you will support me in the future more!