[Python-ideas] Documenting Python warts

Chris Angelico rosuav at gmail.com
Wed Jan 2 10:37:51 CET 2013


On Wed, Jan 2, 2013 at 8:27 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> There would be a lot less confusion if they weren't called "else". Even
> now, I have to explicitly remind myself that the else block doesn't
> run if the for loop is empty, but *after* the for block.
>
> # Python 4000 proposal:
> for x in seq:
>     ...
> then:
>     # this is skipped by a break
> else:
>     # this runs only if seq is empty

Calling it "else" makes perfect sense if you're searching for something.

for x in lst:
  if x.is_what_we_want(): break
else:
  x=thing()
  lst.append(x)

ChrisA



More information about the Python-ideas mailing list