[Python-ideas] except expression
M.-A. Lemburg
mal at egenix.com
Mon Feb 17 16:00:28 CET 2014
On 17.02.2014 15:33, Rob Cliffe wrote:
>
> On 17/02/2014 10:00, M.-A. Lemburg wrote:
>> On 16.02.2014 05:04, Chris Angelico wrote:
>> 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],
>> }
>>
>>
> Just wanted to point out the ambiguity in some of these, at least to a human reader.
This ambiguity is the reason why I pointed at these examples :-)
> If I am not mistaken, the proposed precedence for "except" (higher than "lambda", lower than
> everything else including "if - else") means that:
>
> The first one means
> value = lst[2] except IndexError: (0,1)
> not
> value = (lst[2] except IndexError: 0), 1
The second is what Python would do. You can use if-else for testing
this:
>>> 1 if 2 else 3, 4
(1, 4)
> The second means
> value = lst[2] except IndexError: (0 if x else -1)
> not
> value = (lst[2] except IndexError: 0) if x else -1
Yes.
> The third means
> value = lst[2] except IndexError: (0 if (lst[1] except IndexError: False) else -1)
> rather than anything else (this is getting a bit mind-boggling).
Yes; but I'm not entirely sure if the Python parser could even
handle this case. It also fails on this example:
>>> 1 if 2 if 3 else 4 else 5
File "<stdin>", line 1
1 if 2 if 3 else 4 else 5
^
SyntaxError: invalid syntax
> The fourth means
> value = lst[2] except IndexError: (0 + lst[0])
> rather than
> value = (lst[2] except IndexError: 0) + lst[0]
Yes.
> I wonder if the actual meaning coincides in all cases with your intention when you wrote them?
> No offence intended, MAL, I'm not trying to slag you off, just pointing out some of the issues that
> arise. :-)
Looking at such issues was the intention of posting the list :-)
> And wondering if there is a case for "except" having a higher precedence than "if - else". I don't
> have an opinion yet, I'm just thinking aloud.
>
> The last one is I believe unambiguous except for the value associated with the key 'b', but to my
> mind is another reason for not allowing commas in the proposed "except expression" syntax -
> confusion with commas as separators between dictionary literals (sorry I keep plugging this!).
> There is already ISTM a clash between comma as separator between dictionary items and between tuple
> elements:
>
>>>> { 'a' : (1,3), 'b' : 2 }
> { 'a': (1, 3), 'b': 2 }
>>>> { 'a' : 1,3, 'b' : 2 } # Python 2.7.3
> SyntaxError: invalid syntax
>
> so I think it would be a mistake to overload the comma further.
Commas are indeed too overloaded already.
Guess what these evaluate to :-)
x = lambda y: y, 2
x = 2,
x = 1, 2,
x = {1:2,}
--
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