[Python-ideas] except expression
M.-A. Lemburg
mal at egenix.com
Thu Feb 20 14:45:56 CET 2014
More information about the Python-ideas mailing list
Thu Feb 20 14:45:56 CET 2014
- Previous message: [Python-ideas] except expression
- Next message: [Python-ideas] except expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
On 20.02.2014 02:18, Chris Angelico wrote: > On Thu, Feb 20, 2014 at 11:15 AM, Ethan Furman <ethan at stoneleaf.us> wrote: >> result = 1/x except ZeroDivisionError -> NaN >> >> For the record, I could just as easily live with the colon instead of the >> arrow. >> > > Time to open up this branch of the discussion... colon or arrow? > > For the purposes of this debate, I'm comparing these two notations, > and nothing else: > > result = 1/x except ZeroDivisionError -> NaN > result = 1/x except ZeroDivisionError: NaN I'm -1 on both of them. The colon should stay reserved for starting new blocks of statements. The arrow is used for return type annotations, which is a completely different concept than returning values. I also find it disturbing that people are actually considering to use this expression form as a way to do quick&dirty suppression of exceptions. The intended use case should really be limited to providing default values for functions or methods that are expected to return a value. Abusing the fact that procedures in Python return None (simply because we don't have procedures and need to use functions instead) to make use of except expressions would make code less readable. x = data[1] except IndexError return None # is readable f = open('x.txt', 'r') except IOError return None # is probably not a good idea os.remove('/') except IOError return None # is really bad style The purpose of such except expressions should be to work around small corner cases, not to address important exceptional cases in ones applications. Sometimes I wish we had expression objects in Python to wrap expressions without evaluating them - sort of like lambdas without arguments but with a nicer syntax. These could then be used to implement a default() builtin. Here's a version using lambdas as example: def default(expr, value=None, *exceptions): try: expr() except exceptions: return value x = default(lambda: 1/0, None, ZeroDivisionError) print (x) -- Marc-Andre Lemburg eGenix.com Professional Python Services directly from the Source (#1, Feb 20 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/
- Previous message: [Python-ideas] except expression
- Next message: [Python-ideas] except expression
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-ideas mailing list