is parameter an iterable?

Dave Hansen iddw at hotmail.com
Tue Nov 15 21:17:27 CET 2005


On 15 Nov 2005 11:26:23 -0800 in comp.lang.python, "py"
<codecraig at gmail.com> wrote:

>Dan Sommers wrote:
>> Just do it.  If one of foo's callers passes in a non-iterable, foo will
>> raise an exception, and you'll catch it during testing
>
>That's exactly what I don't want.  I don't want an exception, instead I
>want to check to see if it's an iterable....if it is continue, if not
>return an error code.  I can't catch it during testing since this is
>going to be used by other people.

Then catch the exception yourself.

   >>> def foo2(i):
	try:
		for j in i:
			print j
		print "Success!"
		return 0
	except TypeError, e:
		print "Bad foo.  No donut.", e
		return -1

	
   >>> joe = foo2([1,3,5,7,9])
   1
   3
   5
   7
   9
   Success!
   >>> print joe
   0
   >>> bob = foo2(2)
   Bad foo.  No donut. iteration over non-sequence
   >>> print bob
   -1
   >>> 

Regards,
                                        -=Dave

-- 
Change is inevitable, progress is not.



More information about the Python-list mailing list