Python indentation

Tuure Laurinolli tuure at laurinolli.net
Tue Jul 13 04:42:29 EDT 2004


Antoon Pardon wrote:
> Op 2004-07-12, Mark 'Kamikaze' Hughes schreef <kamikaze at kuoi.asui.uidaho.edu>:
> 
>>Antoon Pardon <apardon at forel.vub.ac.be>
>>wrote on 12 Jul 2004 11:19:50 GMT:
>>
>>>Op 2004-07-09, Mark 'Kamikaze' Hughes schreef <kamikaze at kuoi.asui.uidaho.edu>:
>>>
>>>>  This confuses me as to where the loop starts and ends:
>>>
>>>This confusion may be nothing more than your unfamiliary which
>>>such a construct.
>>>
>>>>linenum=1
>>>>while True:
>>>>    line = raw_input()
>>>>and while line:
>>>>    print "%05d:%s" % (linenum, line,)
>>>>    linenum += 1
>>
>>  No, I am quite certain that my confusion is due to the block
>>indentation aimlessly wandering out, then in, then out again, then in at
>>the end.  And I'm supposed to visually identify that as a single flow of
>>execution?  Bugger that.
> 
> 
> It is as much a single flow of execution as an if elif else.

Not true.

B1: while True:
B1:   line = raw_input()
B2: and while line:
B2:   print "%05d:%s" % (linenum, line)
B2:   linenum += 1

In this example the B1 part is always executed, and B2 is conditionally 
executed dependin on a variable in B1 part.

B1: if v1 == 1:
B1:   line = raw_input()
B2: elif v1 == 1:
B2:   print "%05d:%s" % (linenum, line)
B2:   linenum += 1

In this example the B1 part is always executed, and B2 is never 
executed, even though the test of B2 would be true. The indentation 
denotes the exclusivity of the blocks.

Block structure like that would be extremely confusing.

> 
> 
>>  I've programmed in dozens of languages.  None but BASIC and various
>>assembly languages have a loop structure quite so ugly as that.
> 
> 
> What I don't care about uglyness. I care about implementing algorithms.
> If an algorithm is best expressed in such a kind of loop I prefer to
> implement in such a kind of loop.

The loop used as example above should use an iterator anyway.

>>  Python wins now because it is simple, and clear, and there's one
>>explicitly correct way to solve most tasks.
> 
> No there is not.

Is not simple and clear or doesn't have one explicitly correct way to 
solve most tasks?

> There is nothing incoherent about the proposed structure. A break
> or continue in the middle of a loop is more like a goto and less
> structured than a loop with multiple exit conditions.
> 
> The specific proposed syntax may be ugly, but that doesn't
> make the structure incoherent.

The syntax isn't just ugly, it's confusing and misleading too.



More information about the Python-list mailing list