is parameter an iterable?

marduk usenet at marduk.letterboxes.org
Tue Nov 15 14:26:27 EST 2005


On Tue, 2005-11-15 at 11:01 -0800, py wrote:
> I have function which takes an argument.  My code needs that argument
> to be an iterable (something i can loop over)...so I dont care if its a
> list, tuple, etc.  So I need a way to make sure that the argument is an
> iterable before using it.  I know I could do...
> 
> def foo(inputVal):
>     if isinstance(inputVal, (list, tuple)):
>         for val in inputVal:
>             # do stuff
> 
> ...however I want to cover any iterable since i just need to loop over
> it.
> 
> any suggestions?
> 

You could probably get away with 

if hasattr(inputVal, '__getitem__')





More information about the Python-list mailing list