-2

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  
2
  • 2
    Why couldn't you? You can iterate (for ... in ...) over a lot of things, lists, strings, tuples, range... these don't have next either. Commented May 20 at 2:22
  • 1
    Note that one has to distinguish iterator and iterable. Those two concepts are related but different things in most programming languages. Commented May 20 at 3:13

2 Answers 2

2

doc isn't an iterator. i = iter(doc) creates an iterator if the object can be iterated via the iterable protocol (implements __iter__) or the sequence protocol (implements __getitem__). next(i) gets the next item.

for can iterate over objects that have either of these protocols, but to do it manually with next() requires fetching an iterator with iter().

See iter for more details.

Sign up to request clarification or add additional context in comments.

1 Comment

So for token in doc: is really for token in iter(doc):?
0

Relevant part from python docs:

When using iterables, it is usually not necessary to call iter() or deal with iterator objects yourself. The for statement does that automatically for you, creating a temporary unnamed variable to hold the iterator for the duration of the loop.

So yes, for call iter() on an iterable to obtain an iterator

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.