diff --git a/pydu/itercompat.py b/pydu/itercompat.py new file mode 100644 index 0000000..9895e3f --- /dev/null +++ b/pydu/itercompat.py @@ -0,0 +1,8 @@ +def is_iterable(x): + "An implementation independent way of checking for iterables" + try: + iter(x) + except TypeError: + return False + else: + return True diff --git a/tests/test_itercompat.py b/tests/test_itercompat.py new file mode 100644 index 0000000..ede16a8 --- /dev/null +++ b/tests/test_itercompat.py @@ -0,0 +1,9 @@ +from pydu.itercompat import is_iterable + + +def test_is_iterable(): + assert is_iterable(list()) + assert is_iterable(tuple()) + assert is_iterable(dict()) + assert is_iterable(set()) + assert not is_iterable(1)