python-2.7 - python simpy错误返回生成器内部的值

标签 python-2.7 syntax-error generator return-value

运行以下代码时出现错误

File "simpy.py", line 4
    return 'Monty Pythons Flying Circus'
SyntaxError: 'return' with argument inside generator

这是一个code sample from simpy official site

我尝试了this solution given to a similar looking question,但随后出现以下错误:
Invalid yield value "Monty Pythons Flying Circus"

来自simpy网站的代码:
import simpy
def my_proc(env):
  yield env.timeout(1)
  return 'Monty Pythons Flying Circus'

env = simpy.Environment()
proc = env.process(my_proc(env))
env.run(until=proc)

我按照上面引用的解决方案尝试过的修改后的代码如下:
import simpy
def my_proc(env):
  yield env.timeout(1)
  yield 'Monty Pythons Flying Circus'
  return
env = simpy.Environment()
proc = env.process(my_proc(env))
env.run(until=proc)

此代码给出了错误Invalid yield value "Monty Pythons Flying Circus"
如何解决这个问题?

最佳答案

我遇到了同样的错误,并将其跟踪到代码here
读取Process类的代码和文档字符串:

A generator (also known as a coroutine) can suspend its execution by yielding an event. Process will take care of resuming the generator with the value of that event once it has happened.



我了解问题是您没有产生事件,而是一个字符串

关于python-2.7 - python simpy错误返回生成器内部的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45265981/

相关文章:

python - 如何省略具有空(非零)值的键

objective-c - 消息开始处缺少 '['发送表达式

vba - 使用 VBA 通过所选单元格值的组合过滤表格 - 语法错误

python - 比较多个字典并使用python生成另一个字典

python - Google Drive SDK - 更改 ( ) 键错误

Python Facebook 'NoneType object error'

python - 有时将 float 传递给 int() 方法会出现 ValueError,但并非总是如此

python - Python中赋值错误之前引用的局部变量

python - 在什么情况下你应该在 python 中实际使用生成器?

javascript - NodeJS 生成器永远不会到达某一行?