[Python-ideas] except expression

M.-A. Lemburg mal at egenix.com
Wed Feb 19 10:10:47 CET 2014


On 19.02.2014 08:34, Chris Angelico wrote:
> On Wed, Feb 19, 2014 at 6:25 PM, Paul Moore <p.f.moore at gmail.com> wrote:
>> On 19 February 2014 00:10, Steven D'Aprano <steve at pearwood.info> wrote:
>>> x = d1[key] except K d2[key] except K 0
>>>
>>>
>>> which by the way looks horrible without the colons (sorry Paul, you have
>>> not convinced me that the Tim Peters prohibition on grit applies here).
>>
>> While I did say later on yesterday that I'd become less uncomfortable
>> with the except Whatever: form, I was never advocating just removing
>> the colons - pick your favourite keyword-based proposal and compare
>> with that if you want to see what I was talking about.
>>
>> But that ship has sailed, for better or worse, and it looks like the
>> except-colon form is the winner. So be it.
> 
> The except-colon form is *my* currently-preferred spelling. But when
> Guido took a quick look at it, he didn't like the colon form, and his
> preference makes a lot more difference than mine :) It remains to be
> seen whether the PEP convinces him one way or the other.
> 
> Have you a preferred keyword-based proposal? Does it use an existing
> keyword or create a new one? The floor is yours, Paul: sell me your
> keyword :) Bear in mind, I used to be in favour of the "expr except
> Exception pass default" form, so it shouldn't be too hard to push me
> back to there.

Overall, I think the proposed syntax is too complex and offers
too many options.

The purpose of an except expression would be to solve a single
common problem: that of adding default values for situations
where an exception is raised.

For this purpose, all you need is a very simple form:

    mylist[1] except IndexError return 0

in the same spirit as the conditional expression if - else:

http://docs.python.org/2.7/reference/expressions.html#conditional-expressions

You don't need:
 - support for "as"
 - support for multiple except clauses
 - introduction of a confusing colon block
 - new keywords

If you do need any of these, write a regular try-except block :-)

As grammar you'd get something like this:

conditional_expression ::=  or_test [(if_else_expression | except_expression)]
if_else_expression     ::=  "if" or_test "else" expression
except_expression      ::=  "except" expression "return" expression
expression             ::=  conditional_expression | lambda_expr

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 19 2014)
>>> Python Projects, Consulting and Support ...   http://www.egenix.com/
>>> mxODBC.Zope/Plone.Database.Adapter ...       http://zope.egenix.com/
>>> mxODBC, mxDateTime, mxTextTools ...        http://python.egenix.com/
________________________________________________________________________
2014-02-12: Released mxODBC.Connect 2.0.4 ...     http://egenix.com/go53

::::: Try our mxODBC.Connect Python Database Interface for free ! ::::::

   eGenix.com Software, Skills and Services GmbH  Pastor-Loeh-Str.48
    D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
           Registered at Amtsgericht Duesseldorf: HRB 46611
               http://www.egenix.com/company/contact/


More information about the Python-ideas mailing list