[Python-ideas] if with as

Scott Dial scott+python-ideas at scottdial.com
Sun Mar 4 10:23:22 CET 2007


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.

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.

-Scott

[1] An infix assignment operator would be grammatically add to tests the 
form: test 'as' testlist. This is just a can of worms yielding pure 
obfuscation.

-- 
Scott Dial
scott at scottdial.com
scodial at cs.indiana.edu



More information about the Python-ideas mailing list