while expression feature proposal

Chris Angelico rosuav at gmail.com
Fri Oct 26 20:42:25 EDT 2012


On Sat, Oct 27, 2012 at 10:19 AM, Devin Jeanpierre
<jeanpierreda at gmail.com> wrote:
> On Fri, Oct 26, 2012 at 2:23 AM, Chris Angelico <rosuav at gmail.com> wrote:
>> while (client.spop("profile_ids") as profile_id) is not None:
>>     print profile_id
>>
>> Why is everyone skirting around C-style assignment expressions as
>> though they're simultaneously anathema and the goal? :)
>
> Why should these two statements behave differently? :(
>
>     with foo() as bar: bar.baz()
>     with (foo() as bar): bar.baz()
>
> I don't understand why everyone is so attached to this "as" syntax.
> It's confusing because it behaves subtly differently than how it works
> in "with", and it puts the variable on the wrong side of the
> assignment operator.
>
> (I've always been partial to ":=", personally.)

I'm not attached to "as", myself. It puts the variable at the wrong
end, and feels backward compared to a convention that exists elsewhere
in the language (regular variable assignment). This is a much stronger
backwardness issue than the Python ternary operator (which is only
backward in comparison to other languages).

Personally, I'm quite happy with assignment itself being an
expression. But since that's unlikely to be directly accepted in
Python, I would be looking for something as general as possible -
something that can truly be used _anywhere_ - rather than a specific
while-statement enhancement. Capturing partial expression results is
an extremely convenient thing to do.

ChrisA



More information about the Python-list mailing list