This is very cool, well thought-out, and solves an important problem.  I 
would have definitely benefited from Python having better pattern matching.

As an idealist though, if you're going to add pattern matching, why not 
just do it completely?  I agree that your proposal would be useful in some 
cases, but then what happens when someone wants to go just a step farther?  
For example,

def parse(text):
    if try "(", *a, ")" = text[i, i+n]:   # how do you set n?  do you try 
them all?
        parse(a)  # etc.

and even more complicated:

def parse(text):
   if try "\begin{", *a, "}", *b, "\end{", *a, "}" = ... # how do you 
ensure that the two a's match?  how do you ensure that a can only consist 
of letter characters?

I feel like it would be more idealistic to find a powerful, expressive way 
to specify parsers.  (As far as I know, no Python parser library can 
specify expression yet because they don't execute code on their "rules").  
If we had such a parsing library or extension, then we could compare 
whether it's overkill or reasonable to use that library in situations that 
you're proposing to address with "if try".

Best,

Neil


On Friday, January 3, 2020 at 12:15:42 PM UTC-5, Andrew Barnert via 
Python-ideas wrote:
>
> > On Jan 2, 2020, at 18:34, Dan Sommers <2qdxy4rz...@potatochowder.com 
> <javascript:>> wrote: 
> > 
> > On 1/2/20 3:07 PM, Random832 wrote: 
> >> On Thu, Jan 2, 2020, at 13:22, Dan Sommers wrote: 
> > 
> >>> What about "if except" (any time I can eliminate a "not," that's a 
> >>> good thing): 
> >> ... is this meant to be for the doesn't-throw case or does-throw? ... 
> >>>     if except x: 
> >>>         y 
> > 
> > That would be the does-throw case.  Read it as "if there's an exception 
> > evaluating x, then do the following." 
>
> But then you have the opposite of what you need. You want “if it matches 
> this, do that with these bindings”, not “if it doesn’t match this, do that 
> (with these failed bindings)”. 
>
> Also, you can’t chain these up and keep trying patterns until one 
> succeeds, or default if all of them fail; instead you can only chain them 
> up and keep trying patterns until one fails, or default if they all 
> succeed. 
>
> So, this syntax might be useful for other things, but not for pattern 
> matching. 
>
> >> ... how are we eliminating the not? 
> > 
> > I changed "not try" to "except," thus eliminating the "not." 
>
> But there’s no “not try”, just a “try”. We want to run code if the try 
> condition succeeds, which is positive, not negative. 
>
> Or, if you prefer, it’s a double negative, and you’ve removed one of the 
> negatives: 
>
>    if try (x, 0, z) := vec: 
>        xz_stuff(x, z) 
>
> … would be roughly equivalent to: 
>
>    try: 
>        x, 0, z = vec 
>    except ValueError. 
>        pass 
>    else: 
>        xz_stuff(x, z) 
>
> What you’ve given is a way to put code in the one place where we didn’t 
> want any (but had to put a “pass” just for syntactic reasons) without 
> putting code in the place we did. 
>
>
> _______________________________________________
> Python-ideas mailing list -- python...@python.org <javascript:>
> To unsubscribe send an email to python-id...@python.org <javascript:>
> https://mail.python.org/mailman3/lists/python-ideas.python.org/
> Message archived at 
> https://mail.python.org/archives/list/python-ideas@python.org/message/5HOJJFQIQ25JZLPPGBOZTEMDDTAMGBSJ/
> Code of Conduct: http://python.org/psf/codeofconduct/
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/DC2OKRB6GKQHIIY65XEMHWGK4GKGEJ6L/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to