while expression feature proposal

Ian Kelly ian.g.kelly at gmail.com
Fri Oct 26 11:42:28 EDT 2012


On Fri, Oct 26, 2012 at 9:29 AM, Dan Loewenherz <dloewenherz at gmail.com> wrote:
>     while client.spop("profile_ids") as truthy, profile_id:
>         if not truthy:
>             break
>
>         print profile_id
>
> Here, client.spop returns a tuple, which will always returns true. We then extract the first element and run a truth test on it. The function we use is in charge of determining the truthiness.

I don't like the idea of testing the first element.  There's a large
element of surprise in doing that, I think.  I would expect the truth
test to be the same with or without the existence of the "as" clause
there.  That is, you should be able to remove the "as" clause and have
exactly the same behavior, just without the assignments.  So it would
need to test the entire tuple.

That brings up an interesting additional question in my mind, though.
Should the while loop syntax attempt to perform the assignment on the
very last test, when the expression is false?  I think there is a good
argument for doing so, as it will allow additional inspection of the
false value, if necessary.  In the above, though, if the return value
is false (an empty tuple or None) then the assignment would fail
during unpacking, raising an exception.



More information about the Python-list mailing list