what is the best way to turn a list into bool value? I am looking for something like:
return eval_bool(my_list)
I have a custom container in which I implement the __nonzero__ method which is supposed to work like this:
if self.my_list:
return True
return False
But is it pythonic enough? :) Anyway, I am curious how Python interprets the value of the list in the if statement because this code works differently:
return my_list == True
J.
解决方案
Just use:
bool(my_list)
Which evaluates it as Python "truthiness" and returns a real Boolean.