python isinstance(obj, tpyes.GeneratorType) fails
如果我检查类型
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | 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 |
您的代码读起来有点混乱,因为变量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | 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的