[Python] Iterate over an object that is not an iterator

Discussão em 'Python' iniciado por Stack, Maio 20, 2025.

  1. Stack

    Stack Membro Participativo

    I'm not sure why I can iterate over an object that is not an iterator?

    >>> import spacy
    >>> nlp = spacy.load("en_core_web_sm")
    >>> doc = nlp("Berlin looks like a nice city")
    >>> next(doc)
    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    TypeError: 'spacy.tokens.doc.Doc' object is not an iterator
    >>> for token in doc:
    ... print(token)
    ...
    Berlin
    looks
    like
    a
    nice
    city

    Continue reading...

Compartilhe esta Página