This works:
def f(): pass
import sys
results={'iterable':[],'not iterable':[]}
def isiterable(obj):
try:
it=iter(obj)
return True
except TypeError:
return False
for el in ['abcd',[1,2,3],{'a':1,'b':2},(1,2,3),2,f,sys, lambda x: x,set([1,2]),True]:
if isiterable(el):
results['iterable'].append('\t{}, a Python {}\n'.format(el,type(el).__name__))
else:
results['not iterable'].append('\t{}, a Python {}\n'.format(el,type(el).__name__))
print 'Interable:'
print ''.join(results['iterable'])
print 'Not Interable:'
print ''.join(results['not iterable'])
Prints:
Interable:
abcd, a Python str
[1, 2, 3], a Python list
{'a': 1, 'b': 2}, a Python dict
(1, 2, 3), a Python tuple
set([1, 2]), a Python set
Not Interable:
2, a Python int
<function f at 0x100492d70>, a Python function
<module 'sys' (built-in)>, a Python module
<function <lambda> at 0x100492b90>, a Python function
True, a Python bool
This is more fully explored on this SO post.