You can easily scope Python with five knowledge points

  • 2020-05-10 18:23:38
  • OfStack

1. Block-level scope

Do you think there would be any output if you ran the following program at this point? Will the execution succeed?


# Block-level scopes 
 
if 1 == 1:
 name = "lzl"
 
print(name)
 
 
for i in range(10):
 age = i
 
print(age)

So let's look at the execution first


C:/Users/L/PycharmProjects/s14/preview/Day8/ scope /main.py
lzl
9
 
Process finished with exit code 0

Code execution is successful, no problem; In Java/C#, executing the above code will prompt name, age is not defined, but it can be executed successfully in Python. This is because there is no blocky scope in Python. Variables in the code block can be called externally, so it can be run successfully.

  2, local scope

So just to review what we learned earlier, when we learned about functions, they had a separate scope, so Python doesn't have a block-level scope, but it does have a local scope; Take a look at the code below


# Local scope 
 
def func():
 name = "lzl"
 
print(name)

So let's run this code and see if there's any output?


Traceback (most recent call last):
 File "C:/Users/L/PycharmProjects/s14/preview/Day8/ scope /main.py", line 23, in <module>
 print(name)
NameError: name 'name' is not defined

Run error, I'm sure you all understand this, name variable only in func() The function takes effect internally, so it cannot be called globally. Make a simple tweak to the above code and see what happens.


# Local scope 
 
def func():
 name = "lzl"
 
func()   # Executive function 
print(name)

1 line of code was added to the previous code, and 1 function was executed before the variable name was printed. Will the printing change at this time?


Traceback (most recent call last):
 File "C:/Users/L/PycharmProjects/s14/preview/Day8/ scope /main.py", line 23, in <module>
 print(name)
NameError: name 'name' is not defined

The execution still reports an error, so let's go back to the previous sentence: even if 1 function is executed, the scope of name is only inside the function, and it cannot be called from outside. Keep the first two things in mind, and you're going to start scaling up

3. Scope chain

Adjust the function to see how the following code performs.


# The scope chain 
 
name = "lzl"
def f1():
 name = "Eric"
 def f2():
  name = "Snor"
  print(name)
 f2()
f1()

If you've studied functions, you know the end f1() Output after execution Snor ; Let's first remember the concept that Python has a scope chain, and the variables will be found from the inside to the outside. We first go to our own scope to find the variables, but we don't go to the higher level to find them until we can't find the error

4. Ultimate scope

Okay, that's enough setup. Here comes the final version


# Ultimate scope 
 
name = "lzl"
 
def f1():
 print(name)
 
def f2():
 name = "eric"
 f1()
 
f2()

Do you want to print "lzl" or "eric" when f2() is executed? Remember your answer, but before you post it, take a look at this code:


# Ultimate scope 
 
name = "lzl"
 
def f1():
 print(name)
 
def f2():
 name = "eric"
 return f1
 
ret = f2()
ret()
 
# Output: lzl

The result of execution is "lzl". Analyze the above code. f2() The result is the memory address of the function f1, i.e ret=f1 ; perform ret() Equivalent to performing f1() , the implementation of f1() with f2() It doesn't matter. name=“lzl” with f1() In a scope chain, no variables inside the function will look out, so the value of variable name is "lzl". If you understand that, then you know the answer to the final code that didn't give you the answer


# Ultimate scope 
 
name = "lzl"
 
def f1():
 print(name)
 
def f2():
 name = "eric"
 f1()
 
f2()
 
#  Output: lzl

Yes, the output is "lzl," and remember that before the function is executed, the scope is formed and the scope chain is generated

5. Sina interview questions


C:/Users/L/PycharmProjects/s14/preview/Day8/ scope /main.py
lzl
9
 
Process finished with exit code 0
0

What is the type of li? What are the types of elements in li?


C:/Users/L/PycharmProjects/s14/preview/Day8/ scope /main.py
lzl
9
 
Process finished with exit code 0
1

You can see that li is a list type, and the elements in list are functions. So, if you print the return value of the first element in list, what is the return value?


C:/Users/L/PycharmProjects/s14/preview/Day8/ scope /main.py
lzl
9
 
Process finished with exit code 0
2

The return value of the first li function is 9 and not 0. Remember: the internal code is not executed until the function is executed. The code in the blog can be practiced by yourself to deepen the impression

conclusion

The above is the entire content of this article, I do not know whether to your study and work can bring some help, if you have questions can leave a message to communicate.


Related articles: