| Store | Cart

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

From: ilya <ilya...@gmail.com>
Fri, 7 Aug 2009 15:06:11 +0400
I believe people now discuss this both on python-dev and python-ideas,
though since I'm new to both lists, I can't really tell where this
belongs.

I played a little with this syntax, my try_ function and @catch
decorator (which are at http://mit.edu/~unknot/www/try_cond.py):

    #   x = float(string) except float('nan') if ValueError
    x = try_(float, string, except_ = float('nan'), if_ = ValueError)

    @catch(ValueError = float('nan'))
    def x1(): return float(string)

    #   y = float(string) except ValueError: float('nan')
    y = try_(float, string, { ValueError: float('nan') })

    @catch({ValueError: float('nan')})
    def y1(): return float(string)

    #   try:
    #       z = open(string, 'r')
    #   except IOError as e:
    #       if e.errno == 2:
    #               z = 'not_exist'
    #       else:
    #               raise
    #
    z = try_(open, string, 'r', iocatcher({2: 'no file!'}))

    @catch(iocatcher({2: 'nothing!'}))
    def z1(): return open(string, 'r')

Here are my overall feelings:

(1) it would be interesting to come up with syntax for except/if
clause, but it's not obvious how to make one and this fact itself may
kill the idea.
(2) the more reasonable approach to things like this is by defining a
separate block and then performing a "catch" operation with it.
Unfortunately, this looks very clumsy as currently this can only be
done by defining a separate function. I think code blocks are a good
direction to explore.

2009/8/7 Kristj?n Valur J?nsson <kristjan at ccpgames.com>:
> Unless I am very much mistaken, this is the approach Ruby takes.> Everything is an expression. ?For example, the value of a block is the value of> The last expression in the block.>> I've never understood the need to have a distinction betwen statements and expressions, not when expressions can have side effects. ?It's like that differentce between procedures and functions in pascal that only serves to confuse>> K>> -----Original Message----->> From: python-dev-bounces+kristjan=ccpgames.com at python.org>> [mailto:python-dev-bounces+kristjan=ccpgames.com at python.org] On Behalf>> Of Xavier Morel>> Sent: 6. ?g?st 2009 10:25>> To: python-dev at python.org>> Subject: Re: [Python-Dev] (try-except) conditional expression similar>> to (if-else) conditional (PEP 308)>>>> Wouldn't it be smarter to fix the issue once and for all by looking>> into making Python's compound statements (or even all statements>> without restrictions) expressions that can return values in the first>> place? Now I don't know if it's actually possible, but if it is the>> problem becomes solved not just for try:except: (and twice so for>> if:else:) but also for while:, for: (though that one's already served>> pretty well by comprehensions) and with:.>>>> _______________________________________________> Python-Dev mailing list> Python-Dev at python.org> http://mail.python.org/mailman/listinfo/python-dev> Unsubscribe: http://mail.python.org/mailman/options/python-dev/ilya.nikokoshev%40gmail.com>

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