各位大佬,下面的代码,嵌套函数定义def w():时,如果不传参,输入就符合预期,如果传了上层参数def w(*l):,结果就会报错。
请问传参不传参,影响在哪里呢?
def f(*l):
yield from l
def closure(*l):
a=f(*l)
def w(): # 这里是 def w(*l)下面就会报错,报错内容最下方
nonlocal a
try:
return next(a)
except Exception:
a=f(*l)
return next(a)
return w
c=closure(1,2)
for i in range(5):
print(c())
输出为:
1
2
1
2
1
报错为:
1
2
---------------------------------------------------------------------------
StopIteration Traceback (most recent call last)
<ipython-input-2-5b268514707e> in w(*l)
8 try:
----> 9 return next(a)
10 except Exception:
StopIteration:
During handling of the above exception, another exception occurred:
StopIteration Traceback (most recent call last)
<ipython-input-2-5b268514707e> in <module>
15 c=closure(1,2)
16 for i in range(5):
---> 17 print(c())
18
<ipython-input-2-5b268514707e> in w(*l)
10 except Exception:
11 a=f(*l)
---> 12 return next(a)
13 return w
14
StopIteration: