"Weird" Indentation? (Or: is there a for...else construct?)

  • Thread starter Andreas Waldenburger
  • Start date
A

Andreas Waldenburger

I've found something in the spirit of the following (in the epydoc
sources, if you care):

if True:
print "outer if"
for t in range(2):
if True:
print "for if"
else:
print "phantom else"

For the life of me I can't place the "else". Which if clause does it
belong to? None, it would seem from running the above snippet:

outer if
For if
For if
Phantom else

It seems that there is a for...else construct. Replacing the inner if
with pass seems to confirm this. The else clause is still executed.

What's broken here: Python or my brain?

/W
 
A

Andreas Waldenburger

outer if
For if
For if
Phantom else
Geez, I'm a moron. This is obviously not the output from the snippet.
But if you fix the capitalization, it is. Sorry for that.

/W
 
S

Steven D'Aprano

Andreas said:
It seems that there is a for...else construct. Replacing the inner if
with pass seems to confirm this. The else clause is still executed.

Yes, there is a for...else construct.

The else block runs if the for loop exits *without* a break.

for i in range(20):
if i == 10: break
else:
print "no break here"

for i in range(20):
if i == 100: break
else:
print "no break here"

What's broken here: Python or my brain?

Perhaps we should not answer that question.
 
P

Peter Otten

Andreas said:
I've found something in the spirit of the following (in the epydoc
sources, if you care):

if True:
print "outer if"
for t in range(2):
if True:
print "for if"
else:
print "phantom else"

For the life of me I can't place the "else". Which if clause does it
belong to? None, it would seem from running the above snippet:

outer if
For if
For if
Phantom else

It seems that there is a for...else construct. Replacing the inner if
with pass seems to confirm this. The else clause is still executed.

What's broken here: Python or my brain?

Your rtfm sensor?

http://docs.python.org/reference/compound_stmts.html#the-for-statement

In short, the else suite is executed unless the for-loop is left
via 'break':
.... break
.... else:
.... print "else"
....
.... pass
.... else:
.... print "else"
....
else
Peter
 
A

Andreas Waldenburger

Yes, there is a for...else construct.
That's something. In 6+ years of Python programming I've never seen or
heard of this thing. This might be useful, apparently.

[snip]
What's broken here: Python or my brain?

Perhaps we should not answer that question.
I did phrase that rather provocatively, didn't I?

Well thanks. I'll try to learn less noisily in the future. :)

/W
 
M

MRAB

Andreas said:
That's something. In 6+ years of Python programming I've never seen or
heard of this thing. This might be useful, apparently.
One use case is:

for x in list_of_items:
if x.value == desired_value:
desired_name = x.name
break
else:
print "Couldn't find %s" % x.value
[snip]
What's broken here: Python or my brain?
Perhaps we should not answer that question.
I did phrase that rather provocatively, didn't I?

Well thanks. I'll try to learn less noisily in the future. :)
 
T

Tim Rowe

B

birdsong

That's something. In 6+ years of Python programming I've never seen or
heard of this thing. This might be useful, apparently.
yeah, about 4 years for me and i've never noticed this feature.
[snip]
What's broken here: Python or my brain?
Perhaps we should not answer that question.

I did phrase that rather provocatively, didn't I?

Well thanks. I'll try to learn less noisily in the future. :)

/W
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top