In python, how do I check if an object is a generator object?
Trying this -
>>> type(myobject, generator)
gives the error -
Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name "generator" is not defined
(I know I can check if the object has a
next method for it to be a generator, but I want some way using which I can determine the type of any object, not just generators.)
You can use GeneratorType from types:
>>> import types >>> types.GeneratorType <class "generator"> >>> gen = (i for i in range(10)) >>> isinstance(gen, types.GeneratorType) True
Professional-quality code does more than just run without bugs. It’s clean, readable, and easy to maintain. To step up from a capable Python coder to a professional developer, you need to learn indu...
Vincent Bumgarner has been designing software for nearly 20 years, working in many languages on nearly as many platforms. He started using Splunk in 2007 and has enjoyed watching the product evolve ov...
A Practical Approach to Computer Algorithms Using Python® and C# Rod Stephens started out as a mathematician, but while studying at MIT, he discovered how much fun algorithms are. He took every al...
Scientific progress has increasingly become reliant on large-scale data collection and analysis methodologies. The same is true for the advanced use of computing in business, government, and other are...