<分区>
我正在制作一个 Python irc 机器人。出于某种原因,我的 join() 方法中的 yield 语句使其完全跳过该方法,但如果我用 return 替换它,它就可以正常工作。但是,我需要在每次不成功的加入尝试时产生一个错误。
我有一个机器人的加入方法,如果由于某种原因加入不成功,它会返回服务器错误命令代码响应。如果机器人加入成功,则为 None。
unsuccessful = bot.join(channels)
我能做到:
if unsuccessful:
for error in unsuccessful:
print(error)
连接方法如下所示
def join(self, channels):
chan_errors = range(471, 480) # See RFC for commands 471-479
if isinstance(channels, str):
channels = [channels,]
for channel in channels:
self.send('JOIN %s' % channel)
for response in self.get_response('JOIN', chan_errors): # Verify
if response.command in chan_errors:
channels.remove(channel)
yield response
self.channels.append(channels)
如果我将“ yield 响应”切换为“返回响应”,它就会运行该方法。
get_response 方法看起来像
def get_response(self, commands, terminators=None):
for msg in self.msg_gen():
self.handle(msg)
if msg.command in commands:
if terminators is None:
return msg
yield msg
if msg.command in terminators:
return msg
它从消息生成器接收消息。命令是调用者正在寻找的服务器命令,当找到终止符时,终止符会从生成器中退出。这有点像协程。
有人知道这里发生了什么吗?