我正在尝试检查字典是否为空,但它的行为不正常。它只是跳过它并显示 ONLINE 除了显示消息之外没有任何内容。任何想法为什么?
def isEmpty(self, dictionary):
for element in dictionary:
if element:
return True
return False
def onMessage(self, socket, message):
if self.isEmpty(self.users) == False:
socket.send("Nobody is online, please use REGISTER command" \
" in order to register into the server")
else:
socket.send("ONLINE " + ' ' .join(self.users.keys()))
原文由 Unsparing 发布,翻译遵循 CC BY-SA 4.0 许可协议
空字典在 Python 中 计算为
False
:因此,您的
isEmpty
功能是不必要的。您需要做的就是: