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

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Puddle Jumping Looping

--

Chapters
00:00 Why Or Returns True If The First Element Is True, But Any Still Checks All Element Even If The First
00:27 Accepted Answer Score 2
00:59 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