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)
In general you wouldn't. You'd look for attributes with the names of __iter__
and next
, both functions.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.