File tree Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Expand file tree Collapse file tree 1 file changed +25
-0
lines changed Original file line number Diff line number Diff line change @@ -106,9 +106,34 @@ def test_unique():
106106
107107
108108def test_isiterable ():
109+ # objects that have a __iter__() or __getitem__() method are iterable
110+ # https://docs.python.org/3/library/functions.html#iter
111+ class IterIterable :
112+ def __iter__ (self ):
113+ return iter (["a" , "b" , "c" ])
114+
115+ class GetItemIterable :
116+ def __getitem__ (self , item ):
117+ return ["a" , "b" , "c" ][item ]
118+
119+ # "if a class sets __iter__() to None, the class is not iterable"
120+ # https://docs.python.org/3/reference/datamodel.html#special-method-names
121+ class NotIterable :
122+ __iter__ = None
123+
124+ class NotIterableEvenWithGetItem :
125+ __iter__ = None
126+
127+ def __getitem__ (self , item ):
128+ return ["a" , "b" , "c" ][item ]
129+
109130 assert isiterable ([1 , 2 , 3 ]) is True
110131 assert isiterable ('abc' ) is True
132+ assert isiterable (IterIterable ()) is True
133+ assert isiterable (GetItemIterable ()) is True
111134 assert isiterable (5 ) is False
135+ assert isiterable (NotIterable ()) is False
136+ assert isiterable (NotIterableEvenWithGetItem ()) is False
112137
113138
114139def test_isdistinct ():
You can’t perform that action at this time.
0 commit comments