Is any() evaluated lazily?

前端 未结 7 480
别跟我提以往
别跟我提以往 2021-01-18 03:59

I am writing a script in which i have to test numbers against a number of conditions. If any of the conditions are met i want to return True an

相关标签:
7条回答
  • 2021-01-18 04:42

    Yes, any() and all() short-circuit, aborting as soon as the outcome is clear: See the docs:

    all(iterable)

    Return True if all elements of the iterable are true (or if the iterable is empty). Equivalent to:

    def all(iterable):
        for element in iterable:
            if not element:
                return False
        return True
    

    any(iterable)

    Return True if any element of the iterable is true. If the iterable is empty, return False. Equivalent to:

    def any(iterable):
        for element in iterable:
            if element:
                return True
        return False
    
    0 讨论(0)
提交回复
热议问题