[Python-ideas] except expression

Ron Adam ron3200 at gmail.com
Thu Feb 20 18:31:09 CET 2014



On 02/20/2014 10:17 AM, Chris Angelico wrote:
> On Fri, Feb 21, 2014 at 2:26 AM, Ron Adam<ron3200 at gmail.com>  wrote:
>> >How about re-using the binary operators...
>> >
>> >        (except KeyError try d1[key] | d2[key] | d3[key] | None)
>> >
>> >
>> >It's not uncommon for symbols to mean different things in different places.
>> >
>> >    %, +, *
>> >
>> >There are probably others I'm not thinking of at the moment.
>> >
>> >I really like this actually and it makes the syntax even more concise.:-)

> That works really nicely when you can control the types returned. I've
> written stuff that abuses magic methods (one of the most fun was
> producing something that actually stored an indeterminate value in a
> variable!), but it depends on writing the classes yourself. Can't be
> used in the general sense.
 >
> But if you_can_  control it, all yit was so long agoou need is to do this:
>
> value = d1[key] | d2[key] | d3[key] | None

It needs the syntax part to read correctly, otherwise you would think it's 
a binary or.

And you would still have the issue of early evaluation, so it still needs 
some special help.

The syntax helps to make the meaning clear.

      value = (except IndexError try items[n] | None)

I think with highlighting, this would read very nice and the meaning be 
unambiguous after learning the use of the binary operators in this context. 
  I don't think that would take long as they aren't frequently used symbols.


> and then define d1[key] to return a special object which, when or'd
> with something else, tries to look itself up, and if it fails, returns
> the second object. Actually, here's an even cleaner way. Let's suppose
> those are dictionary-like objects that you fully control. Just do
> this:
>
> value = (d1  | d2 | d3)[key]
>
> and have the "cache | cache" return a helper object that will try one
> and then the other. Could be beautifully clean... but isn't a
> replacement for except-expressions.

I've done something similar with magic methods to implement formula 
evaluation.  With each formula object returning a simplified formula 
object.  (I can't remember the details it was so long ago, but it was a 
hack in any case.)


Just looked at the int and byte types.  Are there magic methods for the 
binary operators?  I don't see them, or I'm overlooking them.  There are 
byte codes, BINARY_OR, etc...  those wouldn't be used in this case.

I don't think this would be hard to implement in byte code.  The "&" 
operation is just executing the two instructions in sequence.  The "|", is 
a standard exception block.  The "!" is a raise exception after the 
expression.  The rest is just were to put those in relation to each other, 
which is handled by the AST.  Nothing very special or difficult to 
implement.  Getting the syntax right is the only tricky part, and that may 
only be because I don't have much experience doing that.

Cheers,
    Ron



More information about the Python-ideas mailing list