The Python Oracle

why OR returns True if the first element is True, but ANY still checks all element even if the first element is True

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Track title: CC C Schuberts Piano Sonata No 13 D

--

Chapters
00:00 Question
00:34 Accepted answer (Score 2)
01:11 Thank you

--

Full question
https://stackoverflow.com/questions/5678...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 2


You're correct, any does short-circuit (only evaluates as much as needed) but as @alfasin said, you're creating the 1/0 error when you try and insert it into a list. To show the delayed evaluation, you'd have to do something like what I put in the comments or

def itr():
    yield 1
    yield 1/0

any(itr())  # --> True

or

class ErrorOnBool:
    def __bool__(self):
        raise RuntimeError()


any([1, ErrorOnBool()])   # --> True