[Python] How to add known length to an iterator?

Discussão em 'Python' iniciado por Stack, Setembro 30, 2024.

  1. Stack

    Stack Membro Participativo

    I make an iterator:

    it = iter([1,2,3])


    len(it) fails because object of type 'list_iterator' has no len().

    I thought I could add a length by doing

    it.__len__ = lambda: 3


    But this also errors 'list_iterator' object has no attribute '__length__'

    How can I set a length on an iterator object?

    Continue reading...

Compartilhe esta Página