0

我正在使用 redis-py 编写一个模块,这里有一个问题:

def get_users_from_usergroup(usergroup):
get_result = r_server.hmget('usergroups', usergroup)
if get_result is not None:
    print('users from the given usegroup [' + usergroup + '] are :')
    print(get_result)
    return (get_result)
else:
   print("Usergroup not found")
   return "error"

我检查了 None 值,但它不起作用我从不进入 else 循环,即使结果是 None。输出 :

users from the given usegroup [random] are :
[None]
users from the given usegroup [random] are :
[None]
users from the given usegroup [random] are :
['as872 bs940 e0286']

我可能错过了一些东西,但我不知道是什么。

4

1 回答 1

1
>>> [None] is None
False

是您问题的答案。尝试将您的条件更改为:

if get_result[0]:
    # there are results
else:
    # there are no results
于 2016-06-09T08:17:42.293 回答