Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the "all" function in Python work?

I searched for understanding about the all function in Python, and I found this, according to here:

all will return True only when all the elements are Truthy.

But when I work with this function it's acting differently:

'?' == True   # False
'!' == True   # False
all(['?','!']) # True

Why is it that when all elements in input are False it returns True? Did I misunderstand its functionality or is there an explanation?

like image 764
ᴀʀᴍᴀɴ Avatar asked Feb 28 '16 17:02

ᴀʀᴍᴀɴ


Video Answer


2 Answers

only when all the elements are Truthy.

Truthy != True.

all essentially checks whether bool(something) is True (for all somethings in the iterable).

>>> "?" == True
False
>>> "?" == False # it's not False either
False
>>> bool("?")
True
like image 173
L3viathan Avatar answered Sep 28 '22 10:09

L3viathan


'?' and '!' are both truthy since they are non-empty Strings.

There's a difference between True and "truthy". Truthy means that when coerced, it can evaluate to True. That's different from it being == to True though.

like image 32
Carcigenicate Avatar answered Sep 28 '22 11:09

Carcigenicate