is parameter an iterable?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Nov 16 06:09:21 EST 2005


On Tue, 15 Nov 2005 21:59:44 -0500, Roy Smith wrote:

> In article <437A9596.6020006 at REMOVEMEcyber.com.au>,
>  Steven D'Aprano <steve at REMOVEMEcyber.com.au> wrote:
> 
>> 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
> 
> That's the obvious solution, but it's a poor one because it depends on an 
> undocumented feature of the language.  What's documented is that TypeError 
> is raised; it's not documented what the text of the message will be.


It would be nice if The Powers That Be would document the specific error
messages, so that we could rely on them in total safety.

But even without that, the consequences aren't especially dire. What
happens if the error message changes in some future version of Python?
Then the error won't be caught, the exception will be re-raised, and your
testing will catch it immediately.


It isn't the ideal solution, but it is a solution.


-- 
Steven.




More information about the Python-list mailing list