[Python-ideas] PEP-3151 pattern-matching
Guido van Rossum
guido at python.org
Mon Apr 25 00:13:09 CEST 2011
On Sun, Apr 24, 2011 at 3:15 AM, Eric Smith <eric at trueblade.com> wrote:
>> Guido van Rossum <guido at python.org> wrote:
>>>On Fri, Apr 8, 2011 at 11:18 AM, Antoine Pitrou <solipsis at
> pitrou.net> wrote:
>>> On Fri, 8 Apr 2011 10:11:34 -0700
>>> Guido van Rossum <guido at python.org> wrote:
>>>> With apologies for not reading the PEP or this thread in full, some
> comments:
>>>>
>>>> - I really like the syntax "except <exc> [as <var>] [if <test>]:".
>>>> This addresses a pretty common use case in my experience. I don't care
>>>> for the alternate proposed syntax that started this thread. I'm not
>>>> sure that the 'if' subclause makes sense without the 'as' subclause,
>>>> since most likely you'd want to refer to the caught exception. I note
>>>> that it is more powerful than putting "if not <test>: raise" in the
>>>> body of the except-clause, because it will allow subsequent except
>>>> clauses to match still. I also note that it is a much "cleaner" change
>>>> than (again) reorganizing the exception hierarchy, since there is no
>>>> backward compatibility to consider.
>>>
>>> My main issue with said new syntax is that it doesn't make things much
>>> easier to write.
>
>>As I explained in other messages, it also adds semantics that are not
>>so easily emulated with the existing syntax (you'd have to repeat code
>>or use nested try/except blocks).
>
> Interestingly, this is one of the few (only?) .NET features that is
> exposed in Visual Basic but not in C#. Maybe there's something to learn
> from that?
>
> http://blogs.msdn.com/b/jaredpar/archive/2008/10/09/vb-catch-when-why-so-special.aspx
FWIW I still am at least +0.5 on the idea. I do note that currently
this is lecal syntax:
try: ...
except stuff if cond else otherstuff: ...
so allowing the syntax
except <exception> [as <variable>] [if <condition>]:
could break some code; however
except <exception> [as <variable> [if <condition>]]:
could not.
Anyone want to write a PEP (could be pretty short & sweet, the VB
reference would be useful) and a reference implementation?
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list