Add is_iterable utility function

This commit is contained in:
Sviatoslav Sydorenko 2018-05-16 16:54:59 +02:00 committed by Sviatoslav Sydorenko
commit 6cafa73441
2 changed files with 48 additions and 1 deletions

View file

@ -15,6 +15,18 @@ def is_string(seq):
return isinstance(seq, (text_type, binary_type))
def is_iterable(seq, include_strings=False):
"""Identify whether the input is an iterable."""
if not include_strings and is_string(seq):
return False
try:
iter(seq)
return True
except TypeError:
return False
def is_sequence(seq, include_strings=False):
"""Identify whether the input is a sequence.