Generalization of any() function with switchable default parameter for empty iterables I have some use-cases where I want the behaviour of the build-in any() function to be slightly different, namely to return True if the iterable is empty.
Therefore I came up with the following override:
_any = any  # Alias built-in any.


def any(iterable, *, default=False):
    &quot;&quot;&quot;Generalization of the built-in any() function.&quot;&quot;&quot;

    if not default:
        return _any(iterable)

    count = 0

    for count, item in enumerate(iterable, start=1):
        if item:
            return True

    return count == 0

The code I used to test this is:
EMPTY_TUPLE = ()
FALSEY_TUPLE = (False, None, 0, '')
TRUEY_TUPLE = (False, None, 1, '')


def empty_iterables():
    &quot;&quot;&quot;Yields empty iterables.&quot;&quot;&quot;

    yield list(EMPTY_TUPLE)
    yield EMPTY_TUPLE
    yield {item: item for item in EMPTY_TUPLE}
    yield set(EMPTY_TUPLE)
    yield (i for i in EMPTY_TUPLE)


def falsey_iterables():
    &quot;&quot;&quot;Yields iterables that any() should assert as False.&quot;&quot;&quot;

    yield list(FALSEY_TUPLE)
    yield FALSEY_TUPLE
    yield {item: item for item in FALSEY_TUPLE}
    yield set(FALSEY_TUPLE)
    yield (i for i in FALSEY_TUPLE)


def truey_iterables():
    &quot;&quot;&quot;Yields iterables that any() should assert as True.&quot;&quot;&quot;

    yield list(TRUEY_TUPLE)
    yield TRUEY_TUPLE
    yield {item: item for item in TRUEY_TUPLE}
    yield set(TRUEY_TUPLE)
    yield (i for i in TRUEY_TUPLE)


def test():
    &quot;&quot;&quot;Tests the custom any() function.&quot;&quot;&quot;

    # Built-in behaviour.
    for iterable in empty_iterables():
        assert any(iterable) is False

    for iterable in falsey_iterables():
        assert any(iterable) is False

    for iterable in truey_iterables():
        assert any(iterable) is True

    # Custom behaviour.
    for iterable in empty_iterables():
        assert any(iterable, default=True) is True

    for iterable in falsey_iterables():
        assert any(iterable, default=True) is False

    for iterable in truey_iterables():
        assert any(iterable, default=True) is True


if __name__ == '__main__':
    test()

I wonder whether there's a more elegant way to implement the custom any() function with the aforementioned behaviour.
Use case
I have a custom monitoring system, that determines the state of a system using check records from a MariaDB database. I use peewee as an ORM:
def check_customer_system(system):
    &quot;&quot;&quot;Returns the customer online check for the respective system.&quot;&quot;&quot;

    end = datetime.now()
    start = end - CUSTOMER_INTERVAL
    condition = OnlineCheck.system == system
    condition &amp;= OnlineCheck.timestamp &gt;= start
    condition &amp;= OnlineCheck.timestamp &lt;= end
    query = OnlineCheck.select().where(condition)

    if query:
        return any(online_check.online for online_check in query)

    raise NotChecked(system, OnlineCheck)

In the current implementation, I need to check, whether the amount of records in the query in non-empty by doing if query:.
However, this will execute the query an load all records into RAM, which I want to avoid. Peewee has the optional .iterator() method on queries, which will return an iterator, that will not load all records into RAM at once. However, if I use that, if query: will always be True since it's testing the truth value of a generator object then.
Hence, I came up with the idea to implement a function, that returns True if any value of an iterable is True or the iterable is empty.
And I just realized that this is indeed an XY-Problem, since I'd loose the ability to distinguish between a checked and a non-checked system.
Thanks @Eric Duminil.
I really think this is a bad idea. A function called any returning True if there not only isn't anything true but even not anything at all... that's just wrong. It sure isn't a &quot;generalization&quot;. Shadowing the built-in is not a good idea, either. Maybe just call yours any_or_empty (without a default)?
For the emptiness check, the counting seems like overkill and I think a Boolean is clearer (and probably faster, due to not going through enumerate):
    empty = True

    for item in iterable:
        if item:
            return True
        empty = False

    return empty

Or with a sentinel, to get rid of the assignment in the loop:
    item = sentinel = object()

    for item in iterable:
        if item:
            return True

    return item is sentinel

For best speed, you could just handle the first item yourself and then let the built-in handle the rest:
def any(iterable, *, default=False):
    &quot;&quot;&quot;Modification of the built-in any() function.&quot;&quot;&quot;

    it = iter(iterable)
    sentinel = object()
    first = next(it, sentinel)
    if first is sentinel:
        return default
    return bool(first) or _any(it)

Your test code uses the words &quot;truey&quot; and &quot;falsey&quot;. That's not Python terminology. See the Python docs, you'll never find those words. It's true and false, as you can for example see at if, Boolean operations and Truth Value Testing.
The name falsey_iterables is ambiguous if not misleading, as it doesn't yield any false iterables. They're all true. They just don't contain anything true. I'd suggest iterables_without_trues. And iterables_with_true instead of truey_iterables for consistency.
Did you have this idea because next, min and max offer a default parameter? In their cases it makes sense, as they rather need non-empty inputs. And meaningful defaults are possible there, for example min with default=float('inf') could be a useful default for the code using the result.