Variable scope in Python

Source: http://www.cnblogs.com/lianzhilei/p/5852959.html

 

1. Block scope

 1 if 1 == 1:
 2     name = "lzl"
 3  
 4 print(name)     //输出lzl
 5  
 6  
 7 for i in range(10):
 8     age = i
 9  
10 print(age)       //输出9

In Java/C#, executing the above code will prompt that name and age are not defined, but it can be executed successfully in Python. This is because there is no block-level scope in Python . The variables in the code block can be called externally. So it can run successfully

 

2. Local scope

1  def   func():
 2      name = " lzl " 
3   
4  print (name) //Throw exception

The name variable only takes effect inside the func() function, so it cannot be called globally

 

3. Scope chain

1 name = "lzl"
2 def f1():
3     name = "Eric"
4     def f2():
5         name = "Snor"
6         print(name)
7     f2()
8 f1()      //输出Snor

There is a scope chain in Python. Variables will be searched from the inside to the outside. First, go to your own scope to find them. You don't go to the superior to find them until you can't find them and report an error.

 

4. Ultimate scope o( ̄▽ ̄)d

 

 1 name = "lzl"
 2  
 3 def f1():
 4     print(name)
 5  
 6 def f2():
 7     name = "eric"
 8     f1()
 9  
10 f2()    //输出lzl

This piece of code can be understood as follows:

 1 name = "lzl"
 2  
 3 def f1():
 4     print(name)
 5  
 6 def f2():
 7     name = "eric"
 8     return f1
 9  
10 ret = f2()
11 ret()

The execution result of f2() is the memory address of the function f1, that is, ret=f1; executing ret() is equivalent to executing f1(), and it has nothing to do with f2() when executing f1(), and name=“lzl” and f1() In a scope chain, if there is no variable inside the function, it will look outside, so the variable name value is "lzl" at this time.

5. One interview question

1 li = [lambda :x for x in range(10)]

Determine the type of li? What is the type of the elements in li?

1 print(type(li))
2 print(type(li[0]))
3  
4 # <class 'list'>
5 # <class 'function'>

You can see that li is a list type, and the elements in the list are functions. Then print the return value of the first element in the list. What is the return value at this time?

1 li = [lambda :x for x in range(10)]
2  
3 res = li[0]()
4 print(res)
5  
6 #输出:9

The return value of the first function of li is 9 and not 0. Remember: the internal code will not be executed before the function is executed;

Parse: This is a list comprehension expression, each element is a function, and each function returns the value of x. for x in range(10) will loop 10 times until the end of x=9, so the value of x is 9, and then generate a list of functions, each function's function is to return the value of x.
res = li[0]()
At this point, the first function in the function list is called, that is, the value of x is returned, and the value of x is already known to be 9
, so the final output is 9,
so the most important point is , in the list comprehension, the only function that is generated by the loop 10 times, she will not return the value of x because it has not been called~ that is, it has not been executed~

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325139151&siteId=291194637