我正在尝试检查一个字典是否为空,但它的行为不正常。它只是跳过它并显示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()))
self.users
是否非空,只需执行if self.users
即可。 - BrenBarnisEmpty
实际上会返回True
,否则返回False
。如果字典为空,则返回None
,它不等于False
。 - Hyperboreus.join(...)
是指socket.join(...)
吗?这是合法的语法吗?如果是的话,我宁愿避免使用它,但如果有相关文档,我仍然很感兴趣了解更多。额,算了吧...这是字符串连接 :D - Nolo