1.5.2 for: else:

John Max Skaller skaller at maxtal.com.au
Wed Aug 4 08:30:52 CEST 1999


On Fri, 30 Jul 1999 02:40:42 GMT, wtanksle at dolphin.openprojects.net (William Tanksley) wrote:

>On Wed, 28 Jul 1999 16:38:24 -0500, Gordon McMillan wrote:
>
>try:
> for x in list:
>   if iffers(x): raise "no!"
>   yadda(x)
>except "no!": pass
>else:
> twiddle()

Not guarranteed to work. Try instead:

exc = "no!"
... raise exc
..
except exc: ..

Python requires the object of a raise to be the same object
as that in the except clause: it is not enough for them to
compare equal as values. Your example only works because
python interns string constants; an implementation detail.

My python interpreter doesn't intern string constants.
The code above will still work, but only because I have extended
the semantics to allow matching by value (which will break
some technically correct python codes).

John Max Skaller                ph:61-2-96600850              
mailto:skaller at maxtal.com.au       10/1 Toxteth Rd 
http://www.maxtal.com.au/~skaller  Glebe 2037 NSW AUSTRALIA




More information about the Python-list mailing list