[Python-ideas] except expression

M.-A. Lemburg mal at egenix.com
Mon Feb 17 11:00:38 CET 2014


On 16.02.2014 05:04, Chris Angelico wrote:
> PEP: XXX
> Title: Exception-catching expressions
> Version: $Revision$
> Last-Modified: $Date$
> Author: Chris Angelico <rosuav at gmail.com>
> Status: Draft
> Type: Standards Track
> Content-Type: text/x-rst
> Created: 15-Feb-2014
> Python-Version: 3.5
> Post-History: 16-Feb-2014
> 
> 
> Abstract
> ========
> 
> Just as PEP 308 introduced a means of value-based conditions in an
> expression, this system allows exception-based conditions to be used as part
> of an expression.

Thanks for writing up this PEP.

> Motivation
> ==========
> 
> A number of functions and methods have parameters which will cause them to
> return a specified value instead of raising an exception.  The current system
> is ad-hoc and inconsistent, and requires that each function be individually
> written to have this functionality; not all support this.
> 
> * dict.get(key, default) - second positional argument in place of KeyError
> 
> * next(iter, default) - second positional argument in place of StopIteration
> 
> * list.pop() - no way to return a default
> 
> (TODO: Get more examples. I know there are some but I can't think of any now.)
> 
> 
> Rationale
> =========
> 
> The current system requires that a function author predict the need for a
> default, and implement support for it. If this is not done, a full try/except
> block is needed.
> 
> Note that the specific syntax is open to three metric tons of bike-shedding.
> 
> The proposal may well be rejected, but even then is not useless; it can be
> maintained as a collection of failed syntaxes for expression exceptions.
> 
> 
> Proposal
> ========
> 
> Just as the 'or' operator and the three part 'if-else' expression give short
> circuiting methods of catching a falsy value and replacing it, this syntax
> gives a short-circuiting method of catching an exception and replacing it.
> 
> This currently works::
>     lst = [1, 2, None, 3]
>     value = lst[2] or "No value"
> 
> The proposal adds this::
>     lst = [1, 2]
>     value = lst[2] except IndexError: "No value"

The colon in there breaks the basic Python concept of having
colons end headers which start a new block of statements:

http://docs.python.org/reference/compound_stmts.html

I think it would be better to have the PEP focus on a solution
that doesn't introduce more lambda like syntax to the language :-)

Please also add examples where the expression notation is
used as part of a larger expression, e.g.

value = lst[2] except IndexError: 0, 1
value = lst[2] except IndexError: 0 if x else -1
value = lst[2] except IndexError: 0 if lst[1] except IndexError: False else -1
value = lst[2] except IndexError: 0 + lst[0]
d = {'a': lst[2] except IndexError: 0,
     'b': lst[1] except IndexError: 0 if lst[0] else 1,
     'c': lst[0],
    }

Or code like this:

try: x = lst[0]
except IndexError: x = lst[2] except Exception: lst[1]

l.sort(key = lambda x: x[1] except IndexError: x[0])

IMO, the colon in there makes the code less readable than the variants
which try to reuse a keyword.

-- 
Marc-Andre Lemburg
eGenix.com

Professional Python Services directly from the Source  (#1, Feb 17 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