[Python-ideas] Combining test and assignment

Chris Rebert pyideas at rebertia.com
Sun Jan 22 19:36:05 CET 2012


On Sun, Jan 22, 2012 at 4:04 AM, Paul Moore <p.f.moore at gmail.com> 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"
>
> 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.

This is reminiscent of Perl's $_ variable, which represents the "current topic".
http://www.perl.com/pub/2002/10/30/topic.html

Cheers,
Chris



More information about the Python-ideas mailing list