帮忙看下这段generator代码

握草 2018-05-26 04:22:45
def psychologist():
print('Please tell me your problems')
while True:
answer = yield
if answer is not None:
if answer.endswith('?'):
print('Don\'t ask yourself too much questions...')
elif 'good' in answer:
print('That\'t good. Go on.')
elif 'bad' in answer:
print('Be positive. Believe in yourself.')
else:
print('Fuck you. What are you talking about!')


谁能解释下下面的输出?

>>> free = psychologist()
>>> next(free)
Please tell me your problems
>>> free.send('how are you?')
Don't ask yourself too much questions
>>> next(free)
>>> next(free)
>>> next(free)
>>> next(free)

为什么只有第一次在next(free)后面出现那行:Please tell me your problems
而后面不管怎么next(free),都没有任何作用(是真的没有任何作用么?)
求解。
...全文
1065 7 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
oyljerry 2018-06-14
  • 打赏
  • 举报
回复
引用 5 楼 qq_40778536 的回复:
就是说程序执行到yiield那一行,并保存当前状态。之后send保存在变量answer里。Right? 这个函数在执行到yield,就被挂起/暂停,之后要进入也是从当前yield的哪一行进入这个生成器函数的,对吗?@oyljerry
对的
握草 2018-06-14
  • 打赏
  • 举报
回复
就是说程序执行到yiield那一行,并保存当前状态。之后send保存在变量answer里。Right? 这个函数在执行到yield,就被挂起/暂停,之后要进入也是从当前yield的哪一行进入这个生成器函数的,对吗?@oyljerry
欢乐的小猪 2018-06-14
  • 打赏
  • 举报
回复
generator.__next__() Starts the execution of a generator function or resumes it at the last executed yield expression. When a generator function is resumed with a __next__() method, the current yield expression always evaluates to None. The execution then continues to the next yield expression, where the generator is suspended again, and the value of the expression_list is returned to __next__()’s caller. If the generator exits without yielding another value, a StopIteration exception is raised. This method is normally called implicitly, e.g. by a for loop, or by the built-in next() function. generator.send(value) Resumes the execution and “sends” a value into the generator function. The value argument becomes the result of the current yield expression. The send() method returns the next value yielded by the generator, or raises StopIteration if the generator exits without yielding another value. When send() is called to start the generator, it must be called with None as the argument, because there is no yield expression that could receive the value. Here is a simple example that demonstrates the behavior of generators and generator functions:
>>> def echo(value=None):
...     print("Execution starts when 'next()' is called for the first time.")
...     try:
...         while True:
...             try:
...                 value = (yield value)
...             except Exception as e:
...                 value = e
...     finally:
...         print("Don't forget to clean up when 'close()' is called.")
...
>>> generator = echo(1)
>>> print(next(generator))
Execution starts when 'next()' is called for the first time.
1
>>> print(next(generator))
None
>>> print(generator.send(2))
2
>>> generator.throw(TypeError, "spam")
TypeError('spam',)
>>> generator.close()
Don't forget to clean up when 'close()' is called.
morningbzy 2018-06-04
  • 打赏
  • 举报
回复
因为你打印完 “Don't ask yourself too much questions” 之后,answer就变成None了,你需要继续send进去
oyljerry 2018-05-26
  • 打赏
  • 举报
回复
yield就是在这个点返回生成器。next就会让代码从yield继续执行。send是发送数据作为yield返回值。
握草 2018-05-26
  • 打赏
  • 举报
回复
这里关键是理解send函数和不直接给出生成内容的yield关键字。 谁来解答下?

37,743

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • WuKongSecurity@BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧