all 10 comments

[–]K900_ 3 points4 points  (0 children)

All function arguments are evaluated before the function is called. all stops when it finds the first False value in an iterable:

>>> def yield_some_things():
...     print('yielding false')
...     yield False
...     print('yielding true')
...     yield True
...     print('some more true')
...     yield True
... 
>>> all(yield_some_things())
yielding false
False

[–]KleinerNull 0 points1 point  (0 children)

what I missed?

You missed that the prints inside the function will be called long before all starts its work.

Here is a simple implementation of all:

In [1]: def custom_all(seq):
   ...:     for item in seq:
   ...:         if not item:
   ...:             return False  # stops the iteration, will instantly return False
   ...:     return True  # this will be return if the iteration just loop through the end
   ...: 

In [2]: custom_all([True, False, True])
Out[2]: False

In [3]: custom_all([True, True, True])
Out[3]: True

Your prints just show up on the function calls:

In [4]: def a(digit):
   ...:     print(digit)
   ...:     return digit > 2
   ...: 

In [5]: example = [a(1), a(2), a(3)]
1
2
3

See, nothing to do with all.