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

2005-04-21 Thread 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

-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-21 Thread Fredrik Lundh
Charles Krug wrote:
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
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-21 Thread 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


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
 
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-21 Thread Roy Smith
Charles Krug  [EMAIL PROTECTED] wrote:
 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.
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-21 Thread Diez B. Roggisch
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?

Yes, thats considered good practice. 
-- 
Regards,

Diez B. Roggisch
-- 
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-21 Thread Steven Bethard
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
--
http://mail.python.org/mailman/listinfo/python-list


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

2005-04-21 Thread John Machin
On Thu, 21 Apr 2005 09:59:54 -0500, Larry Bates
[EMAIL PROTECTED] wrote:

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.

 for wd in alist:
... 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


-- 
http://mail.python.org/mailman/listinfo/python-list