Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does [] and bool return []? [duplicate]

I am trying to return a boolean in a function like this:

return mylist and any(condition(x) for x in mylist)

The behavior should be to return True if the list is empty or if any element in it meets the condition. I am using the first operand as a shortcircuit since any would return True if the list was empty, which is not what I am after.

I would expect [] and boolval to return False since the list is empty, but to my surprise it returns [] whether boolval is True or False. I would expect the first operand to be automatically evaluated as a boolean since it is involved in a comparison operation, and not whatever is happening.

I am not really asking how to solve my problem, which is easily done by an explicit type conversion: bool(mylist), but rather asking what is happening and why.

edit: when I ask "why" this is happening I am not looking for the "facts" only, as they are already explained in the linked duplicate question, but also the reasons behind the implementation of this behavior.

like image 956
dabadaba Avatar asked Mar 06 '23 07:03

dabadaba


1 Answers

The and and or operators do not return True/False. They return the last thing evaluated (that's the case in other dynamic languages too, eg. javascript).

The official documentation describes that

  • for and, the first falsy value, or the last operand
  • for or, the first truthy value, or the last operand

That's by design, so you can create expressions like return username or 'guest'. So, if you want guarantee that a boolean value is returned, you have to

return bool(x or y)

instead of

return x or y
like image 169
blue_note Avatar answered Mar 19 '23 19:03

blue_note