辞書が空かどうかを確認しようとしていますが、正しく動作しません。メッセージを表示するだけで、それをスキップして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()))
isEmpty
実際に返すTrue
辞書から得られた最初のキーがtrueyとリターンがある場合はFalse
そうでありません。辞書が空の場合、それはNone
を返します== False
。
self.users
が空でないかどうかを確認するには、を実行しますif self.users
。