Python indentation deters newbies?

beliavsky at aol.com beliavsky at aol.com
Mon Aug 16 23:54:15 CEST 2004


Peter Hansen <peter at engcorp.com> wrote in message news:<nbudnY5S8Jl2Kb3cRVn-oQ at powergate.ca>...
> Antoon Pardon wrote:
> 
> > Op 2004-08-16, Peter Hansen schreef <peter at engcorp.com>:
> >>Did you really not indent your code in those languages consisntly
> >>anyway?
> > 
> > I can't answer for keith, but my answer is that it depends on
> > what you consider consistent.
> > 
> > My indentation was consistent with the structure of the algorithm.
> > That is not necesarrily the same as the structure you implemeted
> > that algorithm in.
> 
> Huh?  How does an algorithm have structure that affects
> indentation?  It sounds like you are talking about some kind
> of artistic considerations here.
> 
> > I think that I should be the final judge of what is the most
> > appropiate way to use indentation, not the compilor/interpreter,
> > even if it agrees with me.
> 
> So did you indent your code such that consecutive lines were
> not indented to the same identation level even when they were
> conceptually, logically, *algorithmically* part of the same
> block?

I think you are getting a little religious about indentation. I mostly
indent Python the way I'd indent Fortran, but there are some
inflexibilities in Python:

(1) I use a blank line to separate functions. I don't like being
forced to indent every line within the body of a function.

(2) Nested loops can *sometimes* be thought of as a single loop and
indented that way. I don't think the indentation

do i=1,n
do j=1,n
do k=1,n
   x(i,j,k) = some_func(i,j,k)
end do
end do
end do

is necessarily bad. If there were other code between the do
statements, I would indent each loop.

(3) Having a labelled end-of-loop statement lets you exit the desired
number of nested loops in a clean way. In the following Fortran code
you can exit any of the nested loops. How would you simulate this
control flow in Python?

ido: do i=1,n
   ! some code
   if (foo) exit ido ! exit outer loop
   jdo: do j=1,n
      ! some code
      if (boo) exit jdo ! exit middle loop
      do k=1,n
         ! some code
         if (goo) exit ! exit inner loop
      end do
   end do jdo
end do ido



More information about the Python-list mailing list