RFC: Assignment as expression (pre-PEP)

Alex Martelli aleax at mac.com
Tue Apr 10 07:00:26 CEST 2007


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:-).


Alex



More information about the Python-list mailing list