RFC: Assignment as expression (pre-PEP)

Adam Atlas adam at atlas.st
Mon Apr 9 22:23:17 EDT 2007


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.




More information about the Python-list mailing list