How can I verify that a passed argument is an interible collection?
Larry Bates
lbates at syscononline.com
Thu Apr 21 10:59:54 EDT 2005
Others may know better ways but the 2 I know of are:
1) If you know that the arguments will be lists or tuples
you can use isinstance().
if not isinsance(arg, (list, tuple):
print "arg must be list or tuple)
2) Or if you not you could see if the argument has next and
__iter__ methods (more general solution)
if hasattr(arg, 'next') and not hasattr(arg, '__iter__'):
# perform work on iterable
else:
print "arg must be an iterable"
Larry Bates
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?
>
> Thanks
>
>
> Charles
>
More information about the Python-list
mailing list