Python indentation deters newbies?

R

Richard Hanson

[Peter Hansen wrote:]
simo said:
I'm a Perl programmer at heart - well that and PHP, plus the odd
dabbling in C/C++/C# etc.

Anyway, the thought of indentation instead of curly braces really put
me off to start with,

Ah, good. Someone who was really there, instead of hearsay.

[...]

I'm curious why more people don't have "neat!" as their very
first thought on encountering this, rather than "yuck!".

[farther down-thread Peter Hansen wrote:]
For example, if 90% of people who have the "indentation rash"
had previous encounters with FORTRAN IV, then it ought to
be possible to make it obvious in the early documentation
(tutorial, intro page, etc) that Python is not FORTRAN and
doesn't suffer from the same limitations with respect to
indentation/whitespace significance as FORTRAN does.

Not sure if I'm an unqualified newbie, but I'm a relative Python
newbie. When I first encountered Python's indentation, my
reaction was: "Of course!" -- in a seriously head-slapping way.

Neat! -- is putting my reaction mildly. :)

I went on to learn more Python, and now I'm almost a Python
zealot. :)

However, as I mentioned above, I may not qualify as a newbie in
the context of this discussion. I started out programming on IBM
1410s and related mainframes way back in 1968 by punching
programs into stacks of IBM (Hollerith) cards. The first language
we learned in our, first-in-the-district (Portland, Oregon),
high-school "computer" course *was* FORTRAN IV -- laboriously
keypunched into those cards where different columns had different
meanings. (I don't quite remember all the details. I still have
boxes of my rubber-banded-together stacks of cards around,
somewhere, but I'm too lazy to go look for them, now. <wink>)

In any event, I went on to learn React (a PL/1 variant),
miscellaneous BASICs, various desktop and handheld HP calculator
languages, HP71B Basic (very advanced -- had NaN, Inf, and such),
Forth, C, C++, Smalltalk, newer Fortrans, and so on until finally
hitting upon Python several years ago.

For me, a not insignificant part of Python's genius *is* the
indentation. When I used C and C++ I was a stickler (pedant? :)
) for "proper" indentation -- which was a PITA. So -- I took to
Python's indentation like a duck to water.

That all said, I don't know if my story is the type of story a
typical Python newbie would present, but since this thread has
been going on for several day, I thought I'd add another
datapoint. :)

ole-dinosaur-ly y'rs,
Richard
 
R

Richard Hanson

[Re Python's indentation -- "Neat!" or "Yuck!"]

For the record, my first reaction to *braces* years ago, was:
"Yuck!"

;-)

stirring-the-pot-ly y'rs,
Richard
 
P

Peter Hansen

Richard said:
For me, a not insignificant part of Python's genius *is* the
indentation. When I used C and C++ I was a stickler (pedant? :)
) for "proper" indentation -- which was a PITA. So -- I took to
Python's indentation like a duck to water.

That's been exactly my situation as well, which leads me to
theorize that those who react most strongly against Python's
syntactically significant indentation are those who were the
least consistent or, uh, "anal" about making sure their code
conformed to conventions.

The corollary would be that those who already wrote code which
would have been accepted by a compiler which checked indentation,
are those who hardly blink when encountering Python,
other than to say "Of course!" as you did, and carry on writing
code just as they used to, with a mild feeling of dismay that
it never occurred to them before to question the supposed
value of braces or begin/end tags.

-Peter
 
P

Peter Hansen

Richard said:
[Re Python's indentation -- "Neat!" or "Yuck!"]

For the record, my first reaction to *braces* years ago, was:
"Yuck!"

That was my reaction as well, but my sister really needed
them as her teeth weren't straight at all.

-Peter
 
R

Richard Hanson

Peter said:
That's been exactly my situation as well, which leads me to
theorize that those who react most strongly against Python's
syntactically significant indentation are those who were the
least consistent or, uh, "anal" about making sure their code
conformed to conventions.

;-)

obsessive-compulsively y'rs,
Richard
 
S

simo

[snip]
Ah, good. Someone who was really there, instead of hearsay.

Hehe, thought that said "heresy" for a moment! ;o)
Please, *why* did it put you off?

I really don't know, just opposition to change, I'm ashamed to say! I
just couldn't think how you would enclose areas of code without braces
- until you've seen some properly indented Python source, it is hard
to visualise.
Didn't you already indent your code consistently?

Yes, personally, but sometimes it's hard when you're working on code
that has been used by 3 other people with different indentation habits
and even different editors - for example, one chap here uses two
spaces to ident, whereas I've always used 4, nothing like coding
standards eh?!
Didn't you think that the removal of those braces would immediately
make the code more readable (fewer lines, fewer extra cruft to
distract the eye) and easier to type?

The removal of things like $ @ % { etc. appealed to me (then those
fsckers went and put @decorators into 2.4!)
I'm curious why more people don't have "neat!" as their very
first thought on encountering this, rather than "yuck!".

Possibly unfathomable, all I can say is it's opposition to change
(practically every other language does the braces thing) and also it's
hard to visualise until you see a big chunk of Python code - a "Hello
World!" doesn't really show the benefit of removing all that junk.

I have a C++ programer friend who thinks Python is a joke and is not
OOP as it doesn't have proper encapsulation, can you imagine getting
him to lose his braces? (wow that came out wrong!)
 
R

Richard Hanson

Peter said:
Richard said:
[Re Python's indentation -- "Neat!" or "Yuck!"]

For the record, my first reaction to *braces* years ago, was:
"Yuck!"

That was my reaction as well, but my sister really needed
them as her teeth weren't straight at all.

Ha!

(I can almost remember when I used to *have* teeth...)

:-D

laughing-out-loud-heartily y'rs,
Richard
 
M

Mel Wilson

On 13 Aug 2004 07:15:14 -0700, (e-mail address removed) declaimed the following
in comp.lang.python:
Teach them FORTRAN-IV...

A source line is limited to 80 characters
Statements start in column 7
Statements end in column 72 (or was it 71)

Column 72 is the last one available for statment text,
according to this IBM 888157 card here.


Regards. Mel.
 
P

Paramjit Oberoi

It seems to me that you're asking for a rational explanation
Hmm... not for me. The answer would be "because they
taste like dirt", or "I hate purple", or "anything pickled
sucks". "I just don't" looks a lot like a cop-out. If

That's what you would say, but who knows whether that answer would be
correct? It could be that you are simply not comfortable with answers
like "I just don't" which go beyond logic, and thus your brain
manufactures reasonable logical reasons when needed. "I don't know" may
actually be a more honest response than whatever logical response is
offered.

Reasoned, rational reactions are our attempts to understand how we
function. Our nervous system doesn't care about reasoned, rational
reactions.

:)
 
P

Peter Hansen

Paramjit said:
That's what you would say, but who knows whether that answer would be
correct? It could be that you are simply not comfortable with answers
like "I just don't" which go beyond logic, and thus your brain
manufactures reasonable logical reasons when needed. "I don't know" may
actually be a more honest response than whatever logical response is
offered.

If this had much to do with Python at this point, I might respond
that it's easy to verify whether such a logical answer is in fact
correct. For example, serve up some pickled beets that don't taste
like dirt, or die them yellow. If I like them, then it is highly
likely that I was in fact correct about my guess/logical answer...

But really, is there much point in a meta-discussion about
psychology, emotions, and the ways our brains trick ourselves?
Or are you just looking for an argument? :)

-Peter
 
B

beliavsky

Peter Hansen said:
Huh? How does an algorithm have structure that affects
indentation? It sounds like you are talking about some kind
of artistic considerations here.


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
 
P

Peter Hansen

I think you are getting a little religious about indentation.

You bet I am. :) Always have been...
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.

Hmm... without an example, I'm not sure what you suggest. On
the face of it, not having every line indented would strike
me immediately as a serious formatting error that is highly
likely to affect the readability of the code.
(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.

I do! It doesn't show the structure of the code clearly enough.
If there are three loops, there should be three levels of nesting
so that the eye is immediately drawn to it and the mind can say
"aha... possible area for code improvement, look more closely".
Without the indentation, the eye will skip right over that when
scanning lots of text. Probably other reasons too, but suffice
to say that I'd immediately reformat that in any code someone
in my group wrote.
(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

Why with a custom exception, obviously. But what does that have to do
with indentation?

-Peter
 
J

Jorge Godoy

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

Just an attempt and trying to keep it like your code.

.... print 'i =', i # some code...
.... if (i == 5): break # exit outer loop
.... for j in xrange(1, n):
.... print 'j =', j # some code...
.... if (j == 3): break # exit middle loop
.... for k in xrange(1, n):
.... print 'k =', k # some code...
.... if (k == 2): break # exit inner loop
i = 1
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 2
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 3
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 4
j = 1
k = 1
k = 2
j = 2
k = 1
k = 2
j = 3
i = 5

By using "<=" one could have one more iteraction, if this is what is
done with your original code.


Be seeing you,
 
B

beliavsky

Jorge Godoy said:
Just an attempt and trying to keep it like your code.

Thanks. My code did not correctly illustrate breaking out of more than one
level of loop. How would the following code be translated to Python?
It is silly of course, but real-world situations where you want to exit a
nested loop are not that rare.

program xnest_loop
! illustrate breaking a nested loop
integer :: i,j,k,n
n = 4
ido: do i=1,n
jdo: do j=1,n
if (i+j > n) exit ido
do k=1,n
if (i+j-k < 0) exit jdo
print*,i,j,k
end do
end do jdo
end do ido
end program xnest_loop

output:
1 1 1
1 1 2
2 1 1
2 1 2
2 1 3
3 1 1
3 1 2
3 1 3
3 1 4
 
R

Roy Smith

Thanks. My code did not correctly illustrate breaking out of more than one
level of loop. How would the following code be translated to Python?

Ugh. This is a truly ugly scenario. You've got three nested loops,
with each inner loop having a break out of both itself and the
immediately containing loop. Does this correspond to some real-world
algorithm, or is it just a diabolical scenario invented for the example?

The bottom line is that this is really ugly and convoluted logic, and as
such is probably going to be ugly and convoluted no matter what language
you write it in. Unless I don't understand what your code does, I don't
see any way to write it in Python which won't be as ugly or uglier as
your original.
It is silly of course, but real-world situations where you want to exit a
nested loop are not that rare.

Yes, there are occasions where you want nested loops (even three deep),
and need to break out of the innermost one. But I've never seen
anything quite as complex as the scenario you set up here.

What I usually do when I've got a deeply nested loop like that is to
factor it out into its own function, and let the return statement act
like a multi-level break. Something like:

def foo (n):
"""Return the first (i, j, k) triple that meets some
condition. Return None if the condition is never met."""
for i in range (n):
for j in range (n):
for k in range (n):
if condition:
return i, j, k
return None
 
P

Paul Rubin

Thanks. My code did not correctly illustrate breaking out of more than one
level of loop. How would the following code be translated to Python?
It is silly of course, but real-world situations where you want to exit a
nested loop are not that rare.

They're really not all that common.
ido: do i=1,n
jdo: do j=1,n
if (i+j > n) exit ido
...

Well, you could do it with try/raise, like
try:
for i in xrange(1,n+1):
for j in xrange(1, n+1):
if i+j > n: raise 'foo'
...
except 'foo':
...

but more normally you'd just say
for i in xrange(1,n+1):
for j in xrange(1, n-i+1):
...
 
J

Jorge Godoy

Thanks. My code did not correctly illustrate breaking out of more than one
level of loop. How would the following code be translated to Python?

For that I'd adopt either the use of exceptions or a flag. ;-)
It is silly of course, but real-world situations where you want to exit a
nested loop are not that rare.

program xnest_loop
! illustrate breaking a nested loop
integer :: i,j,k,n
n = 4
ido: do i=1,n
jdo: do j=1,n
if (i+j > n) exit ido
do k=1,n
if (i+j-k < 0) exit jdo
print*,i,j,k
end do
end do jdo
end do ido
end program xnest_loop

output:
1 1 1
1 1 2
2 1 1
2 1 2
2 1 3
3 1 1
3 1 2
3 1 3
3 1 4
.... for j in xrange(1, n):
.... if (i + j > n): exit
.... for k in xrange(1, n):
.... if ((i + j - k) < 0):
.... to_exit = 1
.... break
.... print i, '\t', j, '\t', k
.... if to_exit:
.... break
....
1 1 1
1 1 2
2 1 1
2 1 2
2 1 3
3 1 1
3 1 2
3 1 3

Is your last line (3 1 4) correct? In the expression "3 + 1 - 4 < 0" it
is true, so I think the line should be printed... Should it? (Does the
loop run at least once before evaluating the expression?) I really
haven't paid attention to your code, just "translated" it to the above
python, with the same logic as the previous one. Using exceptions would
give me a finer grained control, I think, but would require more lines
of code.



Be seeing you,
 
T

Tim Hochberg

Thanks. My code did not correctly illustrate breaking out of more than one
level of loop. How would the following code be translated to Python?
It is silly of course, but real-world situations where you want to exit a
nested loop are not that rare.

program xnest_loop
! illustrate breaking a nested loop
integer :: i,j,k,n
n = 4
ido: do i=1,n
jdo: do j=1,n
if (i+j > n) exit ido
do k=1,n
if (i+j-k < 0) exit jdo
print*,i,j,k
end do
end do jdo
end do ido
end program xnest_loop

Hmmm. I've never run into a case where the loop logic is that nasty.
And, as it turns out, even the above isn't that nasty if you stare at
it a minute, it can be coded without break/exit at all. So this is how
I'd code it <0.5 wink>:

n=4
for i in range(1,n):
for k in range(1,i+2):
print i, 1, k

Of course this just dodges your point, but I can't be made to care
unless you convince me that I'm likely to run into something like this
in practice.

-tim
 
B

beliavsky

Roy Smith said:
Ugh. This is a truly ugly scenario. You've got three nested loops,
with each inner loop having a break out of both itself and the
immediately containing loop. Does this correspond to some real-world
algorithm, or is it just a diabolical scenario invented for the example?

Ok, here is something short but more realistic and IMO not "ugly".

You may want to exit a nested loop when testing if a condition involving
several variables is met, such as searching for a zero of a multivariate
function.

In Python you can print i,j,k and exit() when m == 0, but in a larger program
you may want more control.

program xnest_loop
! find a Pythagorean triple
n = 5
ido: do i=1,n
do j=1,n
ij = i**2 + j**2
do k=1,n
m = ij - k**2
if (m == 0) exit ido
end do
end do
end do ido
if (m == 0) then
print*,i,j,k
else
print*,"no triple"
end if
end program xnest_loop

There is a thread "Labeled loop break/continue" in comp.std.c about adding
similar functionality to C. Fortran has had a big head start over most other
languages, but they may catch up eventually :).

http://groups.google.com/groups?hl=...32&[email protected]#link1
 
P

Peter Hansen

Antoon said:
I don't like the enforced indentation, even if I
> agree 99.99% of the time with the indentation python enforces.

In my humble opinion, disliking something with which
you disagree only one time in ten thousand is not a
good way to find happiness in life. ;-)


-Peter
 

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

Similar Threads


Members online

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,667
Latest member
DaniloB294

Latest Threads

Top