Evaluate boolean tuple in Python

为君一笑 提交于 2019-12-24 05:39:39

问题


I'm trying to get this to evaluate to false.

(False,)

It's currently equaled to true, because I think the tuple is not empty. So how might one extract or cast this to a boolean? Thanks~


回答1:


Extract the element from the tuple is the most simple approach:

value = (False,)[0]

Python2 is more lenient, but in general it isn't good practice to treat a tuple as a single value for comparison purposes (Python3 explicetly bans it)

Instead, look at the

all

and

any

functions for this behavior. As always, the documentation is your friend:

https://docs.python.org/2/library/functions.html#all



来源:https://stackoverflow.com/questions/26964038/evaluate-boolean-tuple-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!