归还字典

2024-10-03 02:35:55 发布

您现在位置:Python中文网/ 问答频道 /正文

返回我在report函数中构建的字典时遇到一些问题。我知道我实际上在调用decode函数。我就是不知道怎么解决这个问题。一切正常。代码做了它应该做的。但是后来,我添加了这个我想返回的字典,这样我就可以在script_A.py中使用它

调用代码(sript_A.py):

self.decoder = Decoder(Frame)
recvDict = self.decoder.decode(self.message)
print type(recvDict) # this returns <type 'NoneType'>

调用代码(script_B.py):

^{pr2}$

定义processIncomingPacket的其他脚本:

class Some_class(object):
    processIncomingPacket(self, data, callback):
        raise NotImplementedException("not implemented")

更新:

processIncomingPacket的实现:

def processIncomingPacket(self, data, callback):
    _logger.debug(" ".join([hex(ord(x)) for x in data]))
    self.addToFrame(data)
    while self.isFrameReady():
        if self.checkFrame():
            result = self.decoder.decode(self.getFrame())
            if result is None:
                raise PacketIOException("Unable to decode request")
            self.populateResult(result)
            self.advanceFrame()
            callback(result)  # defer or push to a thread?
        else: break

Tags: 函数代码pyselfdata字典typecallback
2条回答

processIncomingPacket似乎什么也没做,只是引发了一个异常。在

class Some_class(object):
    processIncomingPacket(self, data, callback):
       raise NotImplementedException("not implemented")

我相信您需要修复processIncomingPacket的实现,以便对传递的参数执行操作并返回字典。在

问题是processIncomingPacket没有返回传递给它的回调的结果(self.result,在您的例子中):

self.populateResult(result)
self.advanceFrame()
callback(result)  # Doesn't capture the return value

最简单的修复方法是调整回调的工作方式,这样它就不需要返回任何东西。相反,让它获取一个现有的dict对象,然后用您想要的内容更新它:

^{pr2}$

相关问题 更多 >