直接上代码
def l(list):
def d():
return list
return d
#运行
l = l([1,2,3,4])
print l()
这个情况是正常的。
问题在于d函数为什么不能传入list,然后我现在尝试了一下这个代码:
def l(list):
def d(list):
return list
return d
#运行
l = l([1,2,3,4])
#提示错误
print l()
#正常
print l([1,3,5,7,9])
请问这个怎么解释呢?
我个人理解的是,类似于“继承”的情况,当d函数没有参数传入的情况上,基于父函数的参数,当他需要传参了,就覆盖了???
Look at the LEGB principles and you will understand what is going on.
https://zhuanlan.zhihu.com/p/...
Run
l = l([1,2,3,4])
Prompt error
print l() This error is that you did not pass parameters.
In the original code:
The following is for illustration: l in your original code points to a different point:
You may understand if the code is written like this:
Two final points:
Don’t use keywords like
list
as variable namesDon’t overwrite your variable names (unless really necessary)
What the poster needs to understand is: Scope
Case 1: Function l returns the address of function d, which is only used as a return value and has not been called for execution.
The parameter list is passed to l, the scope is within the function l, and can be accessed by d (note that the parameter is passed to l, not d).
If you want to access the list, call as follows:
`