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

C

Charles Krug

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
 
F

Fredrik Lundh

Charles said:
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?

why not leave trapping exceptions to whoever uses your function?

</F>
 
L

Larry Bates

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
 
R

Roy Smith

Charles Krug said:
Do I *coughs* simply *coughs* trap the exception created by:

for v in x:

when v is a scaler quantity?

Sure. Why not? If you're coming from a type-bondage language like
C++ or Java, it probabliy feels strange, but it's the Python way. It
looks like what you want to catch is TypeError.
 
D

Diez B. Roggisch

Charles said:
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?

Yes, thats considered good practice.
 
S

Steven Bethard

Charles said:
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
 
J

John Machin

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"

alist = ['Think', 'before', 'posting!']
hasattr(alist, 'next')
False

===>>> Fails the first leg of your test for iterability.
.... print wd
....
Think
before
posting!

===>>> Looks iterable to me!!

In any case, trying to define "isiterable(x)" in terms of x having or
not having particularly-named attributes doesn't seem to me to be a
very good idea. It's what the method *does* that matters. Other ways
of doing it may be invented. Such a definition has about the same
level of future-proof correlation between cause and effect as "We
waved a dead chicken at the volcano and it stopped erupting".

Cheers,
John
 
T

Terry Reedy

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
[/QUOTE]

The 'not' is a mistake and should be deleted. Iterators *do* have __iter__
methods, as suggested by the previous sentence. But anyway, I believe
strings (or some builtin) still use __getitem__ instead of __next__, so
this test is not adequate for everything.

Terry J. Reedy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,235
Messages
2,571,181
Members
47,818
Latest member
KazukoXea6

Latest Threads

Top