[Python-ideas] Combining test and assignment
p.f.moore at gmail.com
Sun Jan 22 13:04:48 CET 2012
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':
> 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"
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". The need
for assignment comes from the need for a name. You could actually deal
with the if-or-while-expression-assignment case by defining a special
variable __it__ to mean the value of the condition in the containing
if or while expression, but it has all the same lack of generality
issues as any proposal that limits itself to if/while conditions. And
short of explicitly marking the expression somehow, you can't
generalise any further.
More information about the Python-ideas