while expression feature proposal

Devin Jeanpierre jeanpierreda at gmail.com
Fri Oct 26 19:12:17 EDT 2012


On Fri, Oct 26, 2012 at 1:12 AM, Dan Loewenherz <dloewenherz at gmail.com> wrote:
> It seems the topic of this thread has changed drastically from the original message.
>
> 1) "while EXPR as VAR" in no way says that EXPR must be a boolean value. In fact, a use case I've run into commonly in web development is popping from a redis set. E.g.
>
>     client = StrictRedis()
>     while True:
>         profile_id = client.spop("profile_ids")
>         if not profile_id:
>             break
>         print profile_id
>
> 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

For loops are pythonic. You can do this in Python today:

    client = StrictRedis()
    for profile_id in iter(lambda: client.spop("profile_ids"), None):
        pass

I would like a better iter(), rather than a better while loop. It is
irritating to pass in functions that take arguments, and it is
impossible to, say, pass in functions that should stop being iterated
over when they return _either_ a None or a, say, False.

-- Devin



More information about the Python-list mailing list