Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #110739 > unrolled thread
| Started by | jfong@ms4.hinet.net |
|---|---|
| First post | 2016-06-28 18:41 -0700 |
| Last post | 2016-06-29 14:53 +0000 |
| Articles | 4 — 3 participants |
Back to article view | Back to comp.lang.python
"for/while ... break(by any means) ... else" make sense? jfong@ms4.hinet.net - 2016-06-28 18:41 -0700
Re: "for/while ... break(by any means) ... else" make sense? Steven D'Aprano <steve@pearwood.info> - 2016-06-29 12:43 +1000
Re: "for/while ... break(by any means) ... else" make sense? jfong@ms4.hinet.net - 2016-06-29 05:01 -0700
Re: "for/while ... break(by any means) ... else" make sense? Grant Edwards <grant.b.edwards@gmail.com> - 2016-06-29 14:53 +0000
| From | jfong@ms4.hinet.net |
|---|---|
| Date | 2016-06-28 18:41 -0700 |
| Subject | "for/while ... break(by any means) ... else" make sense? |
| Message-ID | <652b0fd3-7c96-4ff6-8dc0-6e7859c838b2@googlegroups.com> |
Anyone who wrote the code below must be insane:-)
for x in range(3):
print(x)
else:
print('I am done')
But here it seems perfectly OK:
for x in range(3):
print(x)
if x == 1: break
else:
print('I am done')
To me, the "else" was bonded with "break" (or return, or raise, or...),
not "for". It make sense:-)
--Jach
[toc] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-06-29 12:43 +1000 |
| Message-ID | <577335dc$0$1588$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #110739 |
On Wed, 29 Jun 2016 11:41 am, jfong@ms4.hinet.net wrote:
> Anyone who wrote the code below must be insane:-)
>
> for x in range(3):
> print(x)
> else:
> print('I am done')
*shrug* Insane or not, it's legal.
import math
pass
import os
pass
import sys
is "insane" too, but still legal. The Python interpreter does not judge your
code.
> But here it seems perfectly OK:
>
> for x in range(3):
> print(x)
> if x == 1: break
> else:
> print('I am done')
>
> To me, the "else" was bonded with "break" (or return, or raise, or...),
> not "for". It make sense:-)
Just ten minutes ago I wrote code that looks like this:
for x in sequence:
try:
do_something(x)
except Error:
continue # try again with the next item
break
else:
default()
The "else" in for...else has nothing to do with any "if" inside the for
block.
--
Steven
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.
[toc] | [prev] | [next] | [standalone]
| From | jfong@ms4.hinet.net |
|---|---|
| Date | 2016-06-29 05:01 -0700 |
| Message-ID | <1960b533-5c3f-4d99-a146-f88d727db8a4@googlegroups.com> |
| In reply to | #110743 |
Steven D'Aprano at 2016/6/29 UTC+8 10:43:52AM wrote: > The "else" in for...else has nothing to do with any "if" inside the for > block. Yes, the "else" has nothing to do with "break" syntactically in Python language, but semantically in English it cause confusion. When I said "insane", I just want to emphasis this situation. "else" should appear only when there is a "break" in the "for" block, then "for ...break... else ..." become perfectly alright semantically. Never think of it in "for ...else ..." or the confusion bond to come up:-) --Jach
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <grant.b.edwards@gmail.com> |
|---|---|
| Date | 2016-06-29 14:53 +0000 |
| Message-ID | <mailman.111.1467212039.2358.python-list@python.org> |
| In reply to | #110743 |
On 2016-06-29, Steven D'Aprano <steve@pearwood.info> wrote:
[...]
> is "insane" too, but still legal. The Python interpreter does not
> judge your code.
That's what Usenet is for.
--
Grant Edwards grant.b.edwards Yow! I'm a nuclear
at submarine under the
gmail.com polar ice cap and I need
a Kleenex!
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web