Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of the scope of variables in Python

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this article "instance Analysis of the scope of variables in Python", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article, "case Analysis of variables in the scope of Python".

1. The concept of acting on

The scope of a variable refers to the scope in which a variable takes effect, and there are two scopes in Python.

Global scope

The global scope is created when the program is executed and destroyed at the end of the program execution. All areas other than functions are global scope. Variables defined in the global scope belong to global variables, and global variables can be accessed anywhere in the program.

Function scope

The function scope is created when the function is called and destroyed at the end of the call. Each call to a function produces a new function scope (not called). Variables defined in the scope of a function are local variables that can only be accessed within the function.

2. Local variables

The so-called local variable is the variable defined inside the function body, that is, it only takes effect inside the function body.

Def testA (): # Local variable a # when assigning values to variables in a function, the default is to assign values to local variables # Local variables will not affect variables outside the function. A = 100 # function body internal access, can access a variable print (a) testA () # 100print (a) # error: name 'a' is not defined

The variable an is a variable defined inside the testA function, and an error is reported immediately when accessed outside the function.

The role of local variables: in the function body, temporarily save the data, that is, when the function call is completed, then destroy the local variable.

3. Global variables

The so-called global variable refers to the variable that can take effect both inside and outside the function.

Think about it: what if there is a data that should be used in both function An and function B?

A: store this data in a global variable.

# define global variable aa = 100def testA (): print (a) # access global variable a, and print data stored in variable a def testB (): print (a) # access global variable a, and print data stored in variable a testA () # 100testB () # 100

Thinking: the testB function requires that the value of the modification variable an is 200. how to modify the program?

A = 100def testA (): print (a) def testB (): a = 200print (a) testA () # 100testB () # 200print (f 'global variable a = {a}') # global variable a = 100

Consider: is the variable an in the a = 200 inside the testB function modifying the global variable a?

A: no. Looking at the above code, it is found that the data of an in line 15 is 100, which is still the value when the global variable an is defined, but does not return.

The 200 inside the testB function. To sum up: a = 200 inside the testB function defines a local variable.

(1) global keyword

Thinking: how to modify global variables within the body of a function?

A = 100def testA (): print (a) def testB (): # if you want to modify the global variable a, the global keyword declares that an is the global variable global an a = 200print (a) testA () # 100testB () # 200print (f 'global variable a = {a}') # global variable a = 200

The function of the global keyword is to declare a variable as a global variable inside the function. In other words, if you want to modify the global variable inside the function, you need to use the global keyword to declare the variable.

(2) Summary

If you assign the variable axi200 directly in the function, an is not a modification of the global variable, but is equivalent to declaring a new local variable inside the function. Modify the global variable within the function body: first global declares an as a global variable, and then the variable is re-assigned.

4. Search for variables

When we use a variable, we first look for it in the current scope, and if there is one, we use the

If not, continue to look in the upper scope, and if so, use the

If still not, continue to look in the upper scope, and so on.

Until the global scope is found and still not found, an exception NameError: name'a'is not defined is thrown.

# exercise description a = 10def fn2 (): def fn3 (): a = 30 print ('fn3:','a =', a) fn3 () print ('fn2:','a =', a) fn2 () "" output: a = 30fn2: a = 10 "" 5, variable data type variable c = 10def fn4 (a) in scope c = 10def fn4 (a): # reassign the parameter in the function Does not affect the other variables a = 20 print ('a =', a, id (a)) fn4 (c) print ('c =', c, id (c)) "output result: a = 20 8791349231264c = 10 8791349230944"# if the data received by the parameter is a global list # when you try to modify the elements in the list within the function The data of the global list will also change c = [1Jing 2jue 3] def fn4 (a): # if the formal parameter executes an object When we modify the object through the parameter # will affect all variables a [0] = 100print ('a =', a, id (a)) fn4 (c) print ('c =', c, id (c)) "" output result: a = [100,2,3] 5132808c = [100,2] 3] 5132808 "" # if we don't change the global variable # we need to use the shallow copy we've learned before # or pass in a slice to solve c = [1, 2, 3] def fn4 (a): # reassign the parameter in the function Will not affect other variables a [0] = 100print ('a =', a, id (a) fn4 (c.copy ()) # fn4 (c [:]) print ('c =', c, id (c)) "output result: a = [100,2,3] 6050824c = [1,2,3] 6050312"6.

Generally speaking, in the actual development process, a program is often composed of multiple functions, and multiple functions share some data, as shown below:

(1) share global variables

# 1. Define global variable glo_num = 0def test1 (): global glo_num # modify global variable glo_num = 100def test2 (): # call the modified global variable print (glo_num) # 2 in the test1 function. Call the test1 function, execute the internal code of the function: declare and modify the global variable test1 () # 3. Call the test2 function to execute the internal code of the function: print test2 () # 100

(2) the return value is passed as a parameter

# first get the return value of function one, and then pass the return value to function two def test1 (): return 50def test2 (num): print (num) # 1. Save the return value of the function test1 result = test1 () # 2. The function return value of the variable as a parameter to the test2 function test2 (result) # 50 is about "the scope of variables in Python instance analysis," the content of this article, I believe we all have a certain understanding, I hope the editor to share the content to help you, if you want to know more related knowledge content, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report