while expression feature proposal

Paul Rubin no.email at nospam.invalid
Fri Oct 26 12:10:58 EDT 2012


Dan Loewenherz <dloewenherz at gmail.com> writes:
> We also don't special case things like this just because x is an empty
> string. If this "while EXPR as VAR" thing were to move forward, we
> shouldn't treat the truth testing any differently than how we already
> do. IMO we should write our applications with the understanding that
> '' will return False and work with that.

We don't "already" treat the truth testing any particular way because we
don't have this construction in the language at the moment.  However,
it's well-established in Python that converting a string to a bool
results in False iff the string is empty.

The empty string is a perfectly good string and code that deals with
strings should handle the empty string properly, unless it knows the
string won't be empty.  Basic modularity principles say to avoid putting
such knowledge into more of the code than necessary.  

The conclusion is to not automatically convert the parameter to a bool.
However, if the "as" can be part of an expression as in Chris Angelico's
post, Chris's suggestion

     while (client.spop("profile_ids") as profile_id) is not None:
         print profile_id

looks good to me.

>     while client.spop("profile_ids") as truthy, profile_id:
>         if not truthy:
>             break

This is ugly on two levels.  First of all, if the .spop() still returns
None at the end of the input, the tuple unpacking will fail.  Second,
the separate test and break defeats the purpose of the "while ... as"
construction.  Might as well use the current style of assignment and
test inside the loop.



More information about the Python-list mailing list