usage
>>> isiterable(MyIterableContainer('Yaruo', 'Yaranaio', 'Yarumi'), 3)
True
>>>
>>> isiterable(MyIterableContainer('Yaruo', 'Yaranaio', 'Yarumi'), 2)
False
>>>
>>>
>>> isiterable([1, 2, 3], 3)
True
>>>
>>> isiterable([1, 2, 3], 2)
False
isiterable
from copy import deepcopy
def isiterable(obj, num_of_elements):
if not(isinstance(num_of_elements, int) \
and num_of_elements >= 0):
raise ValueError('num_of_element sholud \
be zero or a natural number.')
if not hasmethod(obj, '__iter__'): return False
iterator = obj.__iter__()
if not hasmethod(iterator, '__iter__'): return False
if not iterator is iterator.__iter__(): return False
if not hasmethod(iterator, '__next__'): return False
itr = deepcopy(iterator)
for k in range(num_of_elements+1):
try:
itr.__next__()
except StopIteration:
if k < num_of_elements: return False
if k == num_of_elements: pass
except:
return False
else:
if k == num_of_elements: return False
return True
def hasmethod(obj, method_name):
return hasattr(obj, method_name) \
and callable(getattr(obj, method_name))