[Python-ideas] "While" suggestion

Bruce Leban bruce at leapyear.org
Thu Jul 3 20:04:50 CEST 2008

One thing I don't like about using "as" here is that with has very different
semantics for that assignment than while does.

I'd rather have:

    while data in [[my_file.read(1024)]]:

where [[...]] is a shorthand for iter(lambda: ..., None) since at least the
concept of taking an expression and turning it into an iterator is fairly
general. OK, that's pretty ugly so maybe I wouldn't want it. :-) If you add
this to while, you're going to want this in "if" and there are lot of other
cases like:

    while (read_a_number() as data) > 0:

I'd rather see something along the lines of PEP 315.

--- Bruce

On Thu, Jul 3, 2008 at 7:06 AM, Stavros Korokithakis <
stavros at korokithakis.net> wrote:

> Hello all,
> I have noticed that sometimes "while" loops produce "unpythonic" patterns,
> such as the following:
> data = my_file.read(1024)
> while data:
>    do_something(data)
>    data = my_file.read(1024)
> The assignment is repeated, which is less than optimal. Since we don't have
> a while statement that does the check in the end, would it not be better if
> the syntax of while could be amended to  include something like this (in the
> spirit of the new "with" keyword)?:
> while my_file.read(1024) as data:
>   do_something(data)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20080703/8e3a84ff/attachment.html>

More information about the Python-ideas mailing list