Some syntactic sugar proposals

Mark Wooding mdw at distorted.org.uk
Wed Nov 17 11:31:40 EST 2010


Christopher <nadiasvertex at gmail.com> writes:

> i don't like magic names. what about:
>
> t = foo() as v if pred(v) else default_value

This is an improvement on `it'; anaphorics are useful in their place,
but they don't seem to fit well with Python.

But I don't think that's the big problem with this proposal.  The real
problem is that it completely changes the evaluation rule for the
conditional expression.  (The evaluation rule is already pretty screwy:
Python is consistently left-to-right -- except here.)

Evaluating a conditional expression starts in the middle, by evaluating
the condition.  If the condition is true, then it evaluates the
consequent (to the left); otherwise it evaluates the alternative (to the
right).  Screwy, but tolerable.

The proposal is to evaluate the /consequent/, stash it somewhere,
evaluate the condition, and then either output the consequent which we
evaluated earlier or the alternative which we must evaluate now.

Of course, the implementation must be able to tell which of these
evaluation rules to apply.  The marker to look for is either `it'
(original anaphoric proposal -- this is the real reason why `it' should
be a reserved word: its presence radically alters the evaluation rule,
so it ought to be a clear syntactic marker) or `as' (as suggested
above).

Elsewhere in the language, `as' is pretty consistent in what it does:
it provides a name for a thing described elsewhere (a `with' context
object, or an imported thing) -- but nothing else.  It certainly doesn't
suggest a change in the way anything else is evaluated.

        1/x if x != 0 else None

works for any numeric x; it'd be really surprising to me if

        1/x as hunoz if x != 0 else None

didn't.

-1 on this one.

-- [mdw]



More information about the Python-list mailing list