
Scott Dial schrieb:
<keyword> <expression> as <variable>:
Rather, assign the result of the do-something.
these are all just broken versions of an infix assignment operator[1].
Not quite, because the precise do-something is implicit in the keywords. With "import", you assign the imported module (or object), rather than the name (string) that was the expression itself. With "with", you assign the result of the enter call, rather than the context object itself. With except, you assign the caught instance, rather than the tuple of classes that caught it. By strict analogy, in while expr as myvar myvar should be the result of calling bool(expr). Fortunately, that is so useless that people would understand the slight change by analogy to "and" and "or", which return the full value rather than the boolean to which it maps. while expr as myvar: # proposal foo(myvar) <==> while (myvar=expr; bool(myvar)): # except that we can't have inline statements foo(myvar) <==> # current python workaround -- though normally we use object-specific # knowlege to keep it from being quite this ugly. _indirect=[] def _test_and_set(val): _indirect[0] = val return val while _test_and_set(val): foo(_indirect[0])
I have wanted it, though not nearly so often as I have wanted it for the "while" loop. The workarounds with break/continue or changing to a for-loop always bother me. That said, I'm still not sure it would be worth the cost, because people might start trying to write while (1,2,3) and expecting an implicit iteration; the confusion to beginners *might* outweigh the benefits. -jJ

On 3/7/07, Georg Brandl <g.brandl@gmx.net> wrote:
while (1,2,3)
and expecting an implicit iteration; the confusion to beginners *might* outweigh the benefits.
Hm, why would anyone write that because of the new "as" syntax?
It isn't always clear (particularly to a beginner, or someone coming from another programming language) when to use "while" and when to use "for". I've seen plenty of C code with for loops that don't increment a counter -- they could easily be while loops. I imagine getting into it somehow along the following lines; # OK, I want to go through the list. # I need a loop. "while" gives me a loop. while [1, 2, 3] as num: print num # wait, I don't really need the number for this, I just need to get this # stupid thing to loop. Maybe if I take out the "as"? while [1,2,3]: print "got in" Obviously, you can say that the right answer here is a for loop for num in [1, 2, 3]: ... but I'm not sure how hard it would be to explain to a new user. It may be that I'm still thinking pre-file-iterators, and newbies won't have a problem ... but I'm not confident of that. -jJ

On 3/7/07, Georg Brandl <g.brandl@gmx.net> wrote:
while (1,2,3)
and expecting an implicit iteration; the confusion to beginners *might* outweigh the benefits.
Hm, why would anyone write that because of the new "as" syntax?
It isn't always clear (particularly to a beginner, or someone coming from another programming language) when to use "while" and when to use "for". I've seen plenty of C code with for loops that don't increment a counter -- they could easily be while loops. I imagine getting into it somehow along the following lines; # OK, I want to go through the list. # I need a loop. "while" gives me a loop. while [1, 2, 3] as num: print num # wait, I don't really need the number for this, I just need to get this # stupid thing to loop. Maybe if I take out the "as"? while [1,2,3]: print "got in" Obviously, you can say that the right answer here is a for loop for num in [1, 2, 3]: ... but I'm not sure how hard it would be to explain to a new user. It may be that I'm still thinking pre-file-iterators, and newbies won't have a problem ... but I'm not confident of that. -jJ
participants (2)
-
Georg Brandl
-
Jim Jewett