while expression feature proposal
no.email at nospam.invalid
Fri Oct 26 08:06:01 CEST 2012
Dan Loewenherz <dloewenherz at gmail.com> writes:
> In this case, profile_id is "None" when the loop breaks. It would be
> much more straightforward (and more Pythonic, IMO), to write:
> client = StrictRedis()
> while client.spop("profile_ids") as profile_id:
> print profile_id
That is pretty loose, in my opinion. If the loop is supposed to return
a string until breaking on None, the break test should explicitly check
for None rather than rely on an implicit bool conversion that will also
test as false on an empty string. Code that handles strings should do
the right thing with the empty string. What you posted relies on an
unstated assumption that the strings that come back are never empty.
> it's a net negative from just doing things the canonical way (with the
> while / assignment pattern).
Yeah, the while/assignment is a bit ugly but it doesn't come up often
enough to be a bad problem, imho.
More information about the Python-list