How does any and all work in Python?

3

How do the any and all functions work in Python and what are the differences between them?

Are these functions equivalent to Array.prototype.some and Array.prototype.every of Javascript?

    
asked by anonymous 29.03.2017 / 17:05

1 answer

5

I do not know enough about Javascript fundamentals - but superficially yes.

"any" and "all" in Python are functions that receive a sequence or iterator and check the truth condition for each element: once the first element is true, any returns True - if not , it traverses the sequence to the end and returns False .

all does the opposite - returns False in the first element with false value (in Python, any empty sequence or mapping, None, numeric value 0, or a class that implements a% custom method that returns __bool__ ) - and returns False only if all elements have true value.

They can be used directly with a list, iterable or dictionary (in the case of dictionaries, the keys are evaluated), but more usually they are used with a "generator expression" - this is , an expression with a% inline% that does some operation for each element of a sequence.

True for example will return for

The expression that precedes all(chr.isdigit() for chr in "313213882") is equivalent to the body of a callback function, mandatory in languages that do not have the generator expression syntax construction. In Python, if an expression with operators starts to get complicated, just make a function call at the same point (the given example calls a method). You can define a callback function with lambda there, but it would be redundant - and the overhead of the function would be recreated for each element of the sequence:

True - wrong way! : -)

Taking advantage of this to clarify the difference between "generator expression" and "list comprehension":

They are similar - "list comprehension" is when we use the same type expression between brackets ( for and all((lambda chr: chr.isdigit())(chr) for chr in "313213882") ) - then the for is executed immediately, and the end result is a list. There are also set comprehends, which use the same syntax and dict comprehension - almost the same syntax, but have to have two expressions separated by ":" - one for the key, another for the dictionary value. p>

When you limit the expression with parentheses, or, depending on the use, nor is it limited (for example, in a variable assignment) - the correct term is "generator expression" - because the end result is a "generator" : an object that implements the Python iterable protocol, and which will generate a single element every time it is iterated - that is, its internal method is called [ , either explicitly, with the function ] , or implicitly , in __next__ .

For example, in the call to next and for , we do not need extra parentheses or brackets: these functions get an iterable and consume it (not necessarily all of it) - they are generators. Using the brackets in the call (and therefore a list comprehension) will give the same end result, but Python will generate all the elements of the list (and therefore "solve" the expression in square brackets) and call the function: any . Do not do this - as well as potentially spinning the expression to unneeded elements, you're allocating memory for all the elements of the sequence at a time, when you'd probably only need one at a time.

    
29.03.2017 / 17:19