[python-chinese] 在程序中使用yield报错,SyntaxError: 'return' with argument inside generator (xlrdT3a.py, line 246)
helium
helium.sun在gmail.com
星期四 八月 2 10:20:33 HKT 2007
不是不能return,而是return不能带参数
只要你使用了yield,这就不是一个function了,而是个generator,所以返回值是没有意义的。一个单独的return语句触发一个StopIteration异常,表示你这个generator结束。
On 8/2/07, 黄毅 <yi.codeplayer at gmail.com> wrote:
> 用了 yield 就不能用 return
>
>
> On 8/1/07, 张建轶 <farsoftwind at gmail.com> wrote:
> >
> > 由于源程序比较长,而且很复杂
> > 我简化了一下,如下所示:
> >
> > #!/usr/bin/python
> >
> > def ff():
> > for i in range(1,5):
> > print i
> > buffc = []
> > bufbc = []
> > for j in range(1,4):
> > print j
> > buffc.append(u'1')
> > bufbc.append(u'2')
> > print buffc
> > yield buffc
> >
> > for ss in ff():
> > print ss
> >
> > 但这个简化程序正确编译通过,但源程序编译时报错,错误信息如下:
> > SyntaxError: 'return' with argument inside generator ( xlrdT3a.py, line
> 246)
> > 报错的246行的程序只是一句:
> > yield bufxf
> >
> > 请问是怎么回事呢?如何修正?请教
> >
> >
> >
> >
> > _______________________________________________
> > python-chinese
> > Post: send python-chinese at lists.python.cn
> > Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> > Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> > Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
> >
>
>
>
> --
> http://codeplayer.blogspot.com/
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to
> python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to
> python-chinese-request at lists.python.cn
> Detail Info:
> http://python.cn/mailman/listinfo/python-chinese
>
关于邮件列表 python-chinese 的更多信息