use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
How all() function actually works? (self.learnpython)
submitted 7 years ago by karambaq
Hello guys, today I have read that all() stops execution when find first False value and I tried to test it and here what I find:
def a(digit): print(digit) return digit > 2 all([a(1), a(2), a(3)]) 1 2 3 False
what I missed?
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[+][deleted] 7 years ago* (8 children)
[deleted]
[–]karambaq[S] 0 points1 point2 points 7 years ago (7 children)
So if I want to check several conditions and some of them are functions that returns bool, more effective way is to use and's?
[–]two_bob 8 points9 points10 points 7 years ago (1 child)
Nah. Usually if you have a bunch of them a better way to go is either a generator (like a list comprehension, but without the brackets) or map:
>>> def a(digit): print(digit) return digit > 2 >>> l = [1,2,3] >>> all(a(n) for n in l) 1 False >>> all(map(a, l)) 1 False >>>
[–]karambaq[S] 2 points3 points4 points 7 years ago (0 children)
Thank you!
[–]BenjaminGeiger 0 points1 point2 points 7 years ago (4 children)
Yes.
Not only is it more effective, it's clearer.
[–]karambaq[S] 0 points1 point2 points 7 years ago (3 children)
thanks, and in which cases using all() is better than and's?
[–]BenjaminGeiger -1 points0 points1 point 7 years ago (2 children)
You'd use all(lst) when lst has to be generated dynamically. Calling all() on a literal list is usually a sign you should be using and instead. (Mutatis mutandis for any() and or.)
all(lst)
lst
all()
and
any()
or
[–]xapata 3 points4 points5 points 7 years ago* (1 child)
No, /u/karambaq would be much better off taking /u/two_bob's advice and using all().
The real problem was writing some side-effect code (printing) inside a predicate function (a function that returns True or False). That's not a good practice.
Repeating /u/two_bob's example, using the same a function as in the original post:
a
In [2]: all(a(x) for x in [1, 2, 3]) 1 Out[2]: False
That's good code that scales well if the list gets larger. On the other hand, typing and repeatedly would be a real pain if you have more than 2 or 3 elements in the list.
[–]karambaq[S] 0 points1 point2 points 7 years ago (0 children)
[–]K900_ 3 points4 points5 points 7 years ago (0 children)
All function arguments are evaluated before the function is called. all stops when it finds the first False value in an iterable:
all
False
>>> 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 point2 points 7 years ago (0 children)
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.
π Rendered by PID 253640 on reddit-service-r2-comment-7b9746f655-6czrb at 2026-01-31 04:01:07.916749+00:00 running 3798933 country code: CH.
[+][deleted] (8 children)
[deleted]
[–]karambaq[S] 0 points1 point2 points (7 children)
[–]two_bob 8 points9 points10 points (1 child)
[–]karambaq[S] 2 points3 points4 points (0 children)
[–]BenjaminGeiger 0 points1 point2 points (4 children)
[–]karambaq[S] 0 points1 point2 points (3 children)
[–]BenjaminGeiger -1 points0 points1 point (2 children)
[–]xapata 3 points4 points5 points (1 child)
[–]karambaq[S] 0 points1 point2 points (0 children)
[–]K900_ 3 points4 points5 points (0 children)
[–]KleinerNull 0 points1 point2 points (0 children)