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.
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
and
, the first falsy value, or the last operandor
, the first truthy value, or the last operandThat'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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With