Scott Dial schrieb:
Talin wrote:
Personally, I like it - it's an issue that I've brought up before, but your syntax is better. With the introduction of 2.5's "with A as B", and the new exception-handling syntax in Py3K 'except E as v' (and the already existing import syntax), it seems to me that we are, in fact, establishing a general rule that:
<keyword> <expression> as <variable>:
...is a common syntactical pattern in Python, meaning 'do something special with expression, and then as a side effect, assign that expression to the named variable for this block."
While I am not going to advocate it, I would like to point out that these are all just broken versions of an infix assignment operator[1]. As Josiah pointed out, they are used right now in places where explicit assignment is not possible. I don't believe you will ever successfully push such an operator through when it could easily be done explicitly.
The existing uses for "as" are all different. The ones with "except" and "with" do not just assign the left hand expression to the right hand name, but the one with "import" does.
As for this particular case, it is only useful in a very restricted set of expressions and I was only able to find a handful of cases in stdlib where I could drop in a "if x as y". I believe this is an indication of how rarely one wants to do this. YMMV.
It does here. I can't present use cases right now, but I do recall several times where I thought this could have made the intent of the code clearer, I think I even proposed it myself some time ago.
Georg