[Python-ideas] Combining test and assignment

Nick Coghlan ncoghlan at gmail.com
Sun Jan 22 13:37:40 CET 2012


On Sun, Jan 22, 2012 at 10:04 PM, 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"

Yeah, I agree characterising embedded assignments as a more formal
equivalent of natural language pronouns is a useful way to think about
them.

However, one of the other reasons that these proposals tend not to go
anywhere is that they ultimately boil down to some people hating the
idea of having to write something they personally think of as one
operation as a sequence of operations instead. Like multi-line
lambdas, adding embedded assignments doesn't really increase the power
and expressivity of the language as a whole all that much - it just
moves the dividing line between what can be cleanly expressed in a
single statement and what must be expressed as multiple statements a
bit.

Embedded assignments *do* address some instances of the
loop-and-a-half problem, but even there, the more general idiom of
"while True:" + "if exit_cond: break" can handle the situation quite
happily.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list