1

If I check types types.GeneratorType and the generator object text[0] each, both returns <class 'generator'>. However, whenever I use isinstance(), it returns False. What am I doing wrong?

import types
import spacy

nlp = spacy.load('en')
text = [nlp(' '.join(docs)).sents]

print(types.GeneratorType)

Out[27]: <class 'generator'>


print(text)
Out[28]: [<generator object at 0x000001F4407F8950>]

print(type(text[0]))
Out[29]: <class 'generator'>


print(isinstance(text[0], types.GeneratorType))
Out[30]: False
doyouknowkimchi
  • 165
  • 3
  • 12

1 Answers1

1

Your code is a little confusing to read, because the variable text is a list containing a generator object. But I think what you're seeing here comes down to a fairly subtle distinction: the difference between generator functions and generators in Python.

import types
import inspect

def generator_function():
    for i in range(100):
        yield i

generator = (i for i in range(100))

isinstance(generator_function, types.GeneratorType)  # False
isinstance(generator, types.GeneratorType)  # True

inspect.isgeneratorfunction(generator_function)  # True
inspect.isgeneratorfunction(generator)  # False

spaCy's Doc.sents property is a generator function that yields sentence spans – see here for the implementation.

Ines Montani
  • 6,935
  • 3
  • 38
  • 53