RFC: Assignment as expression (pre-PEP)

Georg Brandl g.brandl at gmx.net
Tue Apr 10 04:24:13 EDT 2007


Alex Martelli schrieb:
> Adam Atlas <adam at atlas.st> wrote:
> 
>> Hasn't this been discussed many many times before? I think Guido has
>> been favourable to the idea of allowing :=, but that was a long time
>> ago, and I don't think anything ever came of it.
>> 
>> Personally, if anything, I'd like to see more use of the 'as' keyword
>> as in Python 2.5's new 'with' statement. Assignment is basically what
>> it adds to the statement, so if anything we should reuse it in other
>> statements for consistency.
>> 
>> if my_re1.match(exp) as temp:
>>   # do something with temp
>> elif my_re2.match(exp) as temp:
>>   # do something with temp
>> elif my_re3.match(exp) as temp:
>>   # do something with temp
>> elif my_re4.match(exp) as temp:
>>   # do something with temp
>> 
>> As others have mentioned, your particular instance is probably
>> evidence that you need to restructure your code a little bit, but I do
>> agree that "x = y; if x: ..." is a common enough idiom that it
>> warrants a shortcut. And reusing "as", I think, is nice and readable,
>> and it's an advantage that it doesn't require adding any new keywords
>> or symbols.
> 
> Actually, I agree with you.  Unfortunately, I doubt python-dev will, but
> the chance is good enough that it's probably worth proposing there
> (ideally together with a patch to implement it, just to avoid any
> [otherwise likely] whines about this being difficult to implement:-).

The patch is already done -- I have it lying around here :)

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-list mailing list