I need to check if a certain variable is a generator object. How would I specify the literal generator type in place of the ??? below?
def go():
for i in range(999):
yield i
la = go()
print repr(type(la))
<type 'generator'>
assert type(la) == ???
Use types.GeneratorType
(from the types
module). You should think, though, about why you're doing this. It's usually better to avoid explicit type-checking and just try iterating over the object and see if it works.
import types
assert isinstance(la, types.GeneratorType)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With