| Store | Cart

[Python-Dev] (try-except) conditional expression similar to (if-else) conditional (PEP 308)

From: Steven DAprano <ste...@pearwood.info>
Tue, 11 Aug 2009 09:45:58 +1000
On Tue, 11 Aug 2009 12:29:32 am Dj Gilcrease wrote:
> I figure I would write up the PEP draft, I have never tried writing a> pep before, but i did read PEP 1 and tried to follow it's formating> guides. If there are no additions to the idea, then it seems there> just needs to be a consensus on the syntax before submitting it to> the peps list

Shouldn't there be consensus on whether or not this is a good idea 


> Modivation


>    Often when doing calculations or string recasting (to int, float,>    etc) it is required to wrap the section in a simple try-except>    where the exception just assigns a default value. It would be more>    readable and consise if these type of try-excepts could be written>    on a single line.

Concise (note spelling) certainly, but I question that it would be more 
readable. Newlines are not a bad thing, but trying to squeeze too much 
into a single line is.

When the `x if y else z` expression was first introduced, I was very 
excited because I thought it would be very useful. But I soon found 
that it actually wasn't that useful to me: it was rare that I wanted 
it, and when I did, it was usually more readable to use an `if` block 
instead. So I don't find this proposal the least bit compelling. It 
seems to me to be primarily useful for saving wear and tear on the 
Enter key.

> Syntax Ideas:>    Option 1:>        x = float(string) except float('nan') if ValueError>        op(float(string) except float('nan') if ValueError)

Looks too confusingly like an if test. I find my eye drawn to the final 
clause, `if ValueError`, and expecting that to evaluate to true.


>    Option 2:>        x = float(string) except ValueError: float('nan')>        op(float(string) except ValueError: float('nan'))

At the risk of an extra keyword, I would prefer `unless` instead of 

I find this the least worst of the alternatives. 


>    Option 3:>        x = float(string) except ValueError else float('nan')>        op(float(string) except ValueError else float('nan'))

Also looks confusingly like an if test, but not as strongly as Option 1.


Should the PEP allow expressions like this?

    func(obj) except str(e) if ValueError as e  # Option 1
    func(obj) except ValueError as e: str(e)  # Option 2
    func(obj) except ValueError as e else str(e)  # Option 3

Justify your choice please.

Steven D'Aprano

Recent Messages in this Thread
Jeff McAninch Aug 05, 2009 10:22 pm
Raymond Hettinger Aug 06, 2009 12:59 am
P.J. Eby Aug 06, 2009 01:20 am
Nick Coghlan Aug 06, 2009 10:47 am
Dj Gilcrease Aug 06, 2009 11:18 am
MRAB Aug 06, 2009 11:39 am
Jeff McAninch Aug 06, 2009 02:36 pm
Dino Viehland Aug 06, 2009 09:55 pm
MRAB Aug 06, 2009 11:33 pm
Dino Viehland Aug 07, 2009 12:01 am
MRAB Aug 07, 2009 12:22 am
Jeff McAninch Aug 07, 2009 10:14 am
MRAB Aug 07, 2009 11:03 am
Dj Gilcrease Aug 10, 2009 02:29 pm
Jeff McAninch Aug 06, 2009 02:11 am
Antoine Pitrou Aug 06, 2009 11:32 am
Xavier Morel Aug 06, 2009 10:25 am
Russell E. Owen Aug 06, 2009 07:55 pm
MRAB Aug 07, 2009 12:36 am
Kristján Valur Jónsson Aug 07, 2009 10:22 am
ilya Aug 07, 2009 11:06 am
Michael Foord Aug 07, 2009 11:22 am
Alex Martelli Aug 07, 2009 02:55 pm
Steven DAprano Aug 08, 2009 06:02 am
Xavier Morel Aug 08, 2009 08:17 am
Stephen J. Turnbull Aug 08, 2009 01:19 pm
Alexander Kozlovsky Aug 14, 2009 10:41 pm
Steven DAprano Aug 10, 2009 11:45 pm
Messages in this thread