SoFunction
Updated on 2024-10-28

Parsing local and global variables in python

local variable

What is a local variable

Common definition: a variable defined inside a function is called a local variable.

Without further ado, the code is below:

def test1():
 a = 300 # Define a local variable a and initialize 300
 print("--test1--before modification:a=%s" % a)
 a = 200 # Reassign value to variable a 200
 print("--test1--modified:a=%s" % a)
def test2():
 a = 400 # Define another local variable a, and initialize 400
 print("--test2--modified:a=%s" % a)
# Call the functions test1,test2 respectively.
test1()
test2()

Output:

--test1 -- before modification: a=300
--test1 -- modified: a=200
--test2 -- modified: a=400

Conclusion:

  1. A local variable is a variable defined inside a function.
  2. Variables with the same name can be defined inside different functions, but they have no effect.
  3. The role of local variables, in order to temporarily save data need to be in the need to define variables in the function to store.

global variable

What is a global variable

Definition: A variable is a global variable if it can be used in one function as well as in other functions.

The code is as follows:

a = 100 # Define a global variable a and initialize it to 100
# Define the function test1, test2, the function is to print the value of variable a.
def test1():
 print(a)
def test2():
 print(a)
test1()
test2()
"""
The results are as follows:
100
100
"""

How to change the value of a global variable

First of all, we might think of it like this.

a = 100 # Define a global variable a and initialize it to 100
# Define the functions test1, test2 respectively
def test1():
 print("before modification:a=%s"%a)
 a = 300
 print("modified:a=%s"%a)
def test2():
 print(2)
test1()
test2()
"""

The results are as follows:

UnboundLocalError: local variable 'a' referenced before assignment

Exception Explanation: Local variable a was not defined before it was referenced.

"""
Then we put the functiontest1Amend the content of the paragraph to read as follows:

def test1():
 # print("Before modification: a=%s"%a)
 a = 300
 print("modified:a=%s"%a)

"""
The results are as follows:
modified:a=300
100
"""

This time we found that the error disappeared and the value of the print variable a in function test2 did not change or 100, why is this?

Reason: If a global variable has been redefined within a function, then using the variable within the function will make it a local variable by default; if it has not been defined within the function, then using it directly will be treated as a global variable.

At this point, the function test1 in just defined a local variable with the same name, and did not change the value of the global variable.

Use of global

Now that we can't change the value of a global variable by any means, it's time to recognize the new keyword global, which is used to declare a global variable if it needs to be defined (modified) within a function.

Modify the previous code as follows:

a = 100 # Define a global variable a and initialize it to 100
# Define the functions test1, test2 respectively
def test1():
 global a # Declare that a used in a function is a global variable
 print("before modification:a=%s" % a)
 a = 300
 print("modified:a=%s" % a)
def test2():
 print(2)
test1()
test2()

"""
The results are as follows:
before modification:a=100
modified:a=300
300
"""

This was finally modified successfully.

Global variables of mutable and immutable types

Noticing that the global variable we defined earlier was of numeric type, which is of immutable type, what is the difference in modifying a global variable of mutable type?

a = [100, 200] # Define a list global variable a
# Define the functions test1, test2 respectively
def test1():
 print("before modification:a=%s" % a)
 (300)
 print("modified:a=%s" % a)
def test2():
 print(a)
test1()
test2()
"""

The results are as follows:

before modification:a=[100, 200]
modified:a=[100, 200, 300]
[100, 200, 300]
"""

We found that when a global variable is of a mutable type, it is modified even though we are not declaring the global variable with the global keyword.

Actually:

  • The essence of not being able to modify a global variable when it is not declared with global in a function is that you can't modify the pointing of the global variable, i.e. you can't point the global variable to new data.
  • For global variables of immutable types, global variables cannot be modified when global is not used because the data they point to cannot be modified.
  • For global variables of variable types, since the data they point to can be changed, global variables can be modified when global is not used.

summarize

The above is a small introduction to the python local variables and global variables, I hope to help you, if you have any questions please leave me a message, I will promptly reply to you. Here also thank you very much for your support of my website!
If you find this article helpful, please feel free to reprint it, and please note the source, thank you!