我当前正在使用Python实现以下功能:
if user_can_read(request.user, b) == False:
有没有其他方式来检查函数是否返回 False
?
我当前正在使用Python实现以下功能:
if user_can_read(request.user, b) == False:
有没有其他方式来检查函数是否返回 False
?
你可以直接使用
if user_can_read(request.user, b):
## do stuff
如果 user_can_read
返回任何值(除了0、False等),它都将被视为True,并执行相关操作。
而对于否定情况:if not user_can_read(request.user, b)
bool(-1)
评估为 True
(真)。 - Joel Cornett
function_return_boolean()
返回值为假: - nhahtdhrequest.user
没有权限阅读b
,则返回False
。 - Joran Beasleybool
的实例,而nhahtdh的方法则是如果您特别想与False
进行比较而不仅仅是任何falsy值,则应使用该方法,因为is False
几乎总是一个错误。 - Wooble