Skip to content

Commit 66526e3

Browse files
committed
Added how do I determine if an object is iterable
1 parent 8d0e81f commit 66526e3

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

docs/iterator.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,28 @@ a generator::
4444
1
4545
4
4646

47+
48+
`how do I determine if an object is iterable`_
49+
----------------------------------------------
50+
51+
Duck typing::
52+
53+
try:
54+
iterator = iter(theElement)
55+
except TypeError:
56+
# not iterable
57+
else:
58+
# iterable
59+
60+
Type checking, need at least Python 2.6 and work only for new-style classes::
61+
62+
import collections
63+
64+
if isinstance(theElement, collections.Iterable):
65+
# iterable
66+
else:
67+
# not iterable
68+
69+
4770
.. _iterator - The Python yield keyword explained: http://stackoverflow.com/questions/231767/the-python-yield-keyword-explained
71+
.. _how do I determine if an object is iterable: http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable

0 commit comments

Comments
 (0)