[Python-ideas] Combining test and assignment
Steven D'Aprano
steve at pearwood.info
Sun Jan 22 23:58:26 CET 2012
On Sun, Jan 22, 2012 at 12:04:48PM +0000, Paul Moore wrote:
> On 22 January 2012 04:51, Steven D'Aprano <steve at pearwood.info> wrote:
> > Assignment as an expression feels unnatural to me:
> >
> > if spam() as x != 'ham':
> > frobulate(x)
> >
> > doesn't really correspond to any natural English order.
>
> While in general I agree with you, there *is* a natural reading of this:
>
> "if spam() isn't 'ham', frobulate it"
That's not quite the same, because there is an implicit assignment,
which is ambiguous. Are you frobulating spam(), or 'ham'? Without having
domain-specific knowledge, guessing is risky. E.g. this is easy to
interpret:
If the cat is on the mat, feed it.
since we know that cats can eat but mats don't. But this is not:
If the cat is on the mat, put it in the box.
So I stand by my claim: there is no natural English analog of an
explicit assignment in the middle of the clause.
> The fact that you have to choose a name is because computer languages
> have to be more explicit than natural languages, and can't deal with
> the implicit referent involved in the English usage of "it".
Actually they can. Hypertalk had at least two such implicit variables,
"it" and "the result", with different rules for when each were set. I'm
sure other languages have done similar.
But really, it's not a great idea. The possibility of ambuiguity and
confusion is too great. And what happens when you have two or more such
variables? And in practice, you end up assigning "it" to a named
variable anyway, otherwise it will be overwritten before you get around
to using it.
The CPython interactive interpreter already has one implicit variable, _
which holds the result of the previous command. I don't think Python the
language should follow.
--
Steven
More information about the Python-ideas
mailing list