built in - Python all() and bool() empty cases? -
when using help(all), returns:
all(iterable)=>bool return true if bool(x) true values x in iterable. if iterable empty, return true help(bool) returns:
bool(x) -> bool | | returns true when argument x true, false otherwise. | builtins true , false 2 instances of class bool. | class bool subclass of class int, , cannot subclassed. when trying:
>>>bool() false >>>all([]) true my question is, in case all's input empty list/dict/tuple(i.e. iterator), what's passed bool?? , how come returns true, it's dependent on bool?
bool() never invoked if all()'s argument empty. that's why docs point out behavior of all() on empty input special case.
the behavior bool() == false irrelevant all() in case. way, in python bool subclass of int, bool() == false necessary compatible int() == 0.
as why, e.g., all([]) true, it's preserve useful identities. importantly, non-empty sequence x it's desirable that
all(x) == (bool(x[0]) , all(x[1:])) and all([]) == true result allowing identity hold values of x.
Comments
Post a Comment