Home >Backend Development >Python Tutorial >The difference between global variables and local variables in Python (detailed code explanation)

The difference between global variables and local variables in Python (detailed code explanation)

藏色散人
藏色散人Original
2019-03-16 14:16:095968browse

The difference between global variables and local variables in Python (detailed code explanation)

#Global variables are variables defined and declared outside the function and we need to use them inside the function.

#这个函数使用全局变量s
def f():  
    print s  
  
# 全局作用域
s = "I love Python"
f()

Output:

I love Python

If a variable with the same name is defined within a function scope, then it will only print the value given within the function instead of the global value.

# 这个函数有一个与s同名的变量。
def f():  
    s = "Me too."
    print s  
  
# 全局作用域
s = "I love Python" 
f() 
print s

Output:

Me too
I love Python

Before we call the function f(), the variable s is defined as the string "I love Python". The only statement in f() is the "print s" statement. Since there is no local s, the value of global s will be used.

The question is, what happens if we change the value of s inside function f()? Will it affect the overall situation?

We test it in the following code:

def f():  
    print s 
  
    # 如果我们在下面评论,这个程序不会显示错误。
    s = "Me too."
  
    print s 
  
#全局作用域
s = "I love Python" 
f() 
print s

Output:

Line 2: undefined: Error: local variable 's' referenced before assignment

In order for the above program to work, we need to use the "global" keyword. If we want to assign/change them, we just need to use the global keyword in the function. Printing and accessing do not require globals.

Python "assumes" we want a local variable, so the first print statement throws this error message because of the assignment inside f(). Any variable changed or created inside a function is local if it has not been declared as a global variable. To tell Python that we want to use global variables, we must use the keyword "global"

As shown in the following example:

# 这个函数修改全局变量's' 
def f(): 
    global s 
    print s 
    s = "Look for Python"
    print s  
  
#全局作用域
s = "Python is great!" 
f() 
print s

Output:

Python is great!
Look for Python.
Look for Python.

A nice Example:

a = 1
  
# 使用global,因为没有本地'a' 
def f(): 
    print 'Inside f() : ', a 
  
#变量“a”被重新定义为局部变量
def g():     
    a = 2
    print 'Inside g() : ',a 
  
#使用全局关键字修改全局'a'
def h():     
    global a 
    a = 3
    print 'Inside h() : ',a 
  
# 全局作用域
print 'global : ',a 
f() 
print 'global : ',a 
g() 
print 'global : ',a 
h() 
print 'global : ',a

Output:

global :  1
Inside f() :  1
global :  1
Inside g() :  2
global :  1
Inside h() :  3
global :  3

Related recommendations: "Python Tutorial"

The above is the detailed content of The difference between global variables and local variables in Python (detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn