PEP308: Yet another syntax proposal
Bengt Richter
bokr at oz.net
Tue Feb 11 00:40:12 EST 2003
On Mon, 10 Feb 2003 19:41:54 +0100, holger krekel <pyth at devel.trillke.net> wrote:
[...]
>Now if we had
>
> z = x.getPref() except AttributeError: 'Not specified'
>
>that might add something which we didn't have before
>(in an expression).
That's interesting (I saw it elsewhere also by you ;-)
I am wondering how I could bring that into my current candidate[1]...
z = x.getPref() >!< AttributeError -> 'Not specified'
z = x.getPref() >!< AttributeError -> 'Not specified' >!< NameError -> 'No x binding'
[1] The basic ternary syntax is
C -> x -> y
Sorry to quote myself, but the 308 threads have proliferated to where I'm not sure whether
anyone will notice if an idea is not posed for comparison in more than one place ...
The above is a concise ternary based on a larger concept (from lisp via Paul Moore with my twist):
the concept was [C1 -> v1; C2 -> v2; C3 -> v3; True -> vDefault] I.e., evaluate Ci's until
you find one that's true, then evaluate its companion v. My twist is to drop the brackets
and supstitute '><' instead of ';'. Thus
C1 -> v1 >< C2 -> v2 >< C3 -> v3 >< True -> vDefault
with a single condition, you have
C1 -> v1 >< True -> vDefault
and by making the final condition introducer '><' and its condition (True) optional, you get
C1 -> v1 -> vDefault
which expresses the ternary nicely without keywords.
The latest twist to catch exceptions introduces exception types with ">!<" and attaches result
expressions with the same "->" as for ordinary conditions. Thus:
C1 -> v1 >< C2 -> v2 >< C3 -> v3 >< True -> vDefault >!< E1 -> ev1 >!< E2 -> ev2
and you work left to right, skipping ordinary condition->value pairs if an exception happens.
Ok, this is now my latest-and-greatest variant ;-)
Regards,
Bengt Richter
More information about the Python-list
mailing list