What is the scope of python variables

**Variable scope: **

Generally, variables defined outside the function body become global variables, and variables defined inside the function are called local variables.

All scopes of global variables are readable, local variables can only be read in this function

When a function reads variables, it first reads the local variables of the function itself, and then reads the global variables

Global variables

Read, all readable

Assignment, global

Dictionary, list can be modified

Global variables are all uppercase

E.g

name ='Tim' #Global variable
def f1():
 age =18 #Local variable
 print(age,name)
 def f2():
 age=19 #Local variable
f1()f2()18 Tim
19 Tim

Global variables can also be defined inside the function:

name ='Tim' #Global variable
def f1():
 age =18 #Local variable
 global name #Define global variables
 name ='Eric'print(age,name)f1()print(name)

Global variables are readable by default. If you need to change the value of global variables, you need to use the global definition inside the function

Special: lists, dictionaries, can be modified, but cannot be reassigned, if you need to reassign, you need to use global to define global variables inside the function

NAME =['Tim','mike'] #Global variable
NAME1 =['Eric','Jeson'] #Global variable
NAME3 =['Tom','jane'] #Global variable
def f1():
 NAME.append('Eric') #The append method of the list can change the value of external global variables
 print('Function name:%s'%NAME)
 NAME1 ='123' #Re-assignment cannot change the value of external global variables
 print('Function NAME1: %s'%NAME1)
 global NAME3 #If you need to re-assign the list, you need to use global to define global variables
 NAME3 ='123'print('Function NAME3: %s'%NAME3)f1()print('Outside function name: %s'%NAME)print('Outside function name1: %s'%NAME1)print('Outside function name3: %s'%NAME3)

NAME in the function: ['Tim','mike','Eric']
NAME1 in the function: 123
NAME3 in the function: 123
NAME outside the function: ['Tim','mike','Eric']
NAME1 outside the function: ['Eric','Jeson']
NAME3 outside the function: 123

So far, this article about the scope of python variables is introduced here. For more information about the scope of python variables, please search ZaLou.Cn

Recommended Posts

What is the scope of python variables
What is the use of Python
What is the id function of python
What is the prospect of python development
What is the function body of python
What is the function of adb in python
What is Python variable scope
What is the advantage of python over corporate language
What is the use of python's pip
Where is the pip path of python3
What are the advantages of python language
What is the difference between python and pycharm
Consolidate the foundation of Python (4)
Consolidate the foundation of Python(7)
Consolidate the foundation of Python(6)
Consolidate the foundation of Python(5)
What is introspection in python
What is object-oriented in python
Consolidate the foundation of Python (3)
Minimalism is the soul of Python | Python code to find bugs (10)
The usage of wheel in python
What is list comprehension in python
Python handles the 4 wheels of Chinese
Python simulation of the landlord deal
​What are the numbers in Python?
The premise of Python string pooling
Secrets of the new features of Python 3.8
The father of Python joins Microsoft
The operation of python access hdfs
The usage of tuples in python
End the method of running python
What is the difference between a python script and a web page
Understanding the meaning of rb in python
Can Python implement the structure of the stack?
Learn the basics of python interactive mode
Logistic regression at the bottom of python
Solve the conflict of multiple versions of python
Python implements the sum of fractional sequences
Two days of learning the basics of Python
What is an anonymous function in Python
The essence of Python language: Itertools library
The specific method of python instantiation object
python3 realizes the function of mask drawing
What system is good for self-study python
What is a sequence table in Python
Where is the directory after python installation
What kind of work can python do
The specific method of python import library
Solve the conflict of multiple versions of python
Detailed explanation of the principle of Python super() method
Python 3.9 is here!
The difference between the syntax of java and python
7 features of Python3.9
Python realizes the development of student management system
What does the tab key in python mean
Python implements the shuffling of the cards in Doudizhu
The meaning and usage of lists in python
Solve the problem of python running startup error
Can the value of the python dictionary be modified?
Python implements the source code of the snake game
Detailed explanation of the usage of Python decimal module