This library has some useful types and functions used to get types of iterables. Whilst iterable types were purposely avoided by the types library, iterables offer unique types which could sometimes be useful to obtain.
Start by importing the module:
import itertypes
There are various functions which follow the naming convention is[type]iterator
.
itertypes.isstringiterator(iter("string"))
>>> True
dictionary = {1: 2, 3: 4}
iteration = iter(dictionary.items())
itertypes.isdictitemiterator(iteration)
>>> True
There's also the itertypes.isiterator
function where it checks if the given
object is iterable or not.
my_variable = 5
itertypes.isiterable(my_variable)
>>> False
my_variable = ['a', 'b', 'c', 'd']
all([itertypes.isiterable(my_variable), itertypes.islistiterator(iter(my_variable))])
>>> True
Finally, there's various types which resolve to iterable types. These types can be used with isinstance, or with the equality operator and the type function.
byte = bytearray()
isinstance(iter(byte), itertypes.BytearrayIteratorType)
>>> True
iterable = iter({1: 2, 3: 4}.values())
isinstance(iterable, itertypes.DictValueIteratorType)
>>> True
Install through pip.
python -m pip install itertypes