[Python] Is there a generic python iterator other than iter()?

Discussão em 'Python' iniciado por Stack, Setembro 4, 2025.

  1. Stack

    Stack Membro Participativo

    I wrote this generic iterator generator function because I haven't seen anything like it:

    def genericIterator(item, next):
    while item:
    yield item
    item=next(item)


    I use it for iterating over not-real-collections from C bindings, like this:

    for item in genericIterator(thing.newest_item(), lambda i: i.older()):
    print(f"Another one to checkout, brains: {item.name}")


    I am surprised that I can't find a built-in iterator helper that takes an initial item, plus a means to get the next item from that. iter() does not do it.

    Have I missed anything (maybe apart from the bad form of naming the 2nd parameter next)?

    Continue reading...

Compartilhe esta Página