is parameter an iterable?

Steven D'Aprano steve at REMOVEMEcyber.com.au
Tue Nov 15 21:12:38 EST 2005


Roy Smith wrote:

> You can't tell the difference between:
> 
> try:
>   for i in 5:
>     print i + 1
> except TypeError:
>   print "non-iterable"
> 
> and
> 
> try:
>   for i in ["one", "two", "three"]:
>     print i + 1
> except TypeError:
>   print "can't add string and integer"


try:
     for item in obj:
         do_stuff(item)
except TypeError, msg:
     if msg == "iteration over non-sequence":
         handle_non_iterator()
     else:
         # re-raise the exception
         raise



-- 
Steven.




More information about the Python-list mailing list