0

Can some explain to me what difference the print function makes to the StopIteration error? I heard it has to do with generator internals, but I still don't see how the print would make it work.

import simhash
import re
jabb = '''        Twas brillig, and the slithy toves          Did gyre and gimble in the wabe:        All mimsy were the borogoves,          And the mome raths outgrabe.        "Beware the Jabberwock, my son!          The jaws that bite, the claws that catch!        Beware the Jubjub bird, and shun          The frumious Bandersnatch!"        He took his vorpal sword in hand:          Long time the manxome foe he sought --        So rested he by the Tumtum tree,          And stood awhile in thought.        And, as in uffish thought he stood,          The Jabberwock, with eyes of flame,        Came whiffling through the tulgey wood,          And burbled as it came!        One, two! One, two! And through and through          The vorpal blade went snicker-snack!        He left it dead, and with its head          He went galumphing back.        "And, has thou slain the Jabberwock?          Come to my arms, my beamish boy!        O frabjous day! Callooh! Callay!'          He chortled in his joy.        `Twas brillig, and the slithy toves          Did gyre and gimble in the wabe;        All mimsy were the borogoves,          And the mome raths outgrabe.'''
pope = '''There once was a man named 'Pope'        Who loved an oscilloscope          And the cyclical trace          Of their carnal embrace        Had a damned-near infinite slope!'''
tokej = re.split(r'\W+', 
jabb.lower(), 
flags=re.UNICODE)
tokep = re.split(r'\W+', pope.lower(), flags=re.UNICODE)
#this one works
shinji=[print(shingle) for shingle in simhash.shingle(''.join(tokej), 4)]
#but this one doesn't
shinji=[shingle for shingle in simhash.shingle(''.join(tokej), 4)]

Output

...dadada...
['g', 'r', 'a', 'b']
['r', 'a', 'b', 'e']
---------------------------------------------------------------------------
StopIteration                             Traceback (most recent call last)
/usr/local/lib/python3.7/dist-packages/simhash/__init__.py in shingle(tokens, window)
     17     while True:
---> 18         yield [next(it) for it in its]

2 frames
StopIteration: 

The above exception was the direct cause of the following exception:

RuntimeError                              Traceback (most recent call last)
<ipython-input-23-1ccd829c7f86> in <listcomp>(.0)
      5 tokej = re.split(r'\W+', jabb.lower(), flags=re.UNICODE)
      6 tokep = re.split(r'\W+', pope.lower(), flags=re.UNICODE)
----> 7 shinji=[print(shingle) for shingle in simhash.shingle(''.join(tokej), 4)]
      8 shinji=[shingle for shingle in simhash.shingle(''.join(tokej), 4)]

RuntimeError: generator raised StopIteration

1 Answers1

1

Ok, so it turns out the Stop iteration error gets thrown at the very end of the iterator, but since I was using a print function the output was the same as if the error occured the line after, since it's done print everything already. So some older python code throws errors to stop generators. Which newer python doesn't like so they stopped all of 'that'.