RFC: Assignment as expression (pre-PEP)

G

Georg Brandl

Alex said:
Adam Atlas said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top