How can I verify that a passed argument is an interible collection?

Steven Bethard steven.bethard at gmail.com
Thu Apr 21 11:07:43 EDT 2005


Charles Krug wrote:
> List:
> 
> I'm working on some methods that operate on (mathematical) vectors as
> in:
> 
> def Convolution(x, y)
> """Returns a list containing the convolution of vectors x and y"""
> 
> Is there any way to determine at runtime that x and y are iterible
> collections?
> 
> Do I *coughs* simply *coughs* trap the exception created by:
> 
>     for v in x:
> 
> when v is a scaler quantity?

Sure, or if you want to do it earlier, you could try something like:

def convolution(x, y):
     xiter = iter(x)
     yiter = iter(y)

And then use xiter and yiter in your for loops.  This will make sure 
that the TypeErrors get raised as soon as the function is called.

STeVe



More information about the Python-list mailing list