add itercompat.is_iterable for checking iterables

This commit is contained in:
Prodesire 2017-11-02 22:49:26 +08:00
parent bc499168ad
commit 77bab1f07f
2 changed files with 17 additions and 0 deletions

8
pydu/itercompat.py Normal file
View File

@ -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

9
tests/test_itercompat.py Normal file
View File

@ -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)