Python indentation

S

Sateesh

Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

Cheers!
Sateesh
 
R

Reinhold Birkenfeld

Sateesh said:
Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

Yes. It's about readability.

In many languages, such as C or Perl, you can write readable code, but
you can also squeeze your code in as few lines as possible, resulting in
hard to read, hard to maintain, hard to debug code[1]. Whenever I
translate a Perl script into Python, I end up with about 25-40% more
lines, but the script is much more readable than the Perl counterpart
(mostly, of course, because of the lacking $'s ;).

Furthermore, most languages don't force indentation and use { } or
begin-end syntax. In Python, you have to indent your code properly,
which leads to clear structure.

I know how you feel when you are confronted with Python's indentation
system at first: Oh my god, the language is using significant
whitespace. As most of us come from a C background, an absolute
free-form language, this may seem like an unacceptable restraint, but as
you write code, it flows naturally, as if you ever hadn't written
something else. And, you don't have to write "end" :)

Reinhold

[1] This doesn't mean that you cannot write ununderstandable code in
Python... *grin*
 
B

Brian Quinlan

Sateesh said:
Why can't the indentation not so strict so as to give better freedom to the
user?

Can you specify what desirable construction Python is denying you? Or
are you talking about freedom in the abstract?

Cheers,
Brian
 
S

Sateesh

I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code. Also
indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

Sateesh
 
J

Jeff Epler

I doubt anybody but you has ever wondered about this, and if they have
the answers probably don't appear in a FAQ, the tutorial, "google"
or "google groups" searches, or any of the books published about the
Python language.

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA6+SlJd01MZaTXX0RAgKgAJ9JwwpYkjy5DIGU6p5QfK0m7/0lowCfeyDE
ncX4vs1DCCyfgHW5GbCnUMk=
=y1+2
-----END PGP SIGNATURE-----
 
S

Sateesh

I think I would be able to better appreciate Python after I go ahead with
some programming first.

Thanks for the insights!

Sateesh
 
R

Reinhold Birkenfeld

Sateesh said:
I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code. Also
indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

That is what I wrote about. In the beginning, you think of the
indentation as a restriction. Later, you will be glad about it, and you
will not desire to structure your code otherwise.

Reinhold
 
B

Brian Elmegaard

Sateesh said:
I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code. Also
indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

It is probably a matter of taste, but I don't like all the {} and ;
in c and the like.

Indentation in python may be done as you like, just keep it aligned
in the same block.
 
B

Brian Quinlan

Sateesh said:
I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code.

I'm not sure why this is relevant.
Also indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

Which is why I asked you to name a desirable code construction that
Python denies you due to it's indentation rules. If, for example, you
find the following style compelling:

if condition:
clause1
else:
clause2

Then Python is, indeed, restricting you. So could you provide an actual
example where Python is keeping you from using a style that you find
desirable?

Cheers,
Brian
 
V

Ville Vainio

Sateesh> Why can't the indentation not so strict so as to give
Sateesh> better freedom to the user?

You already have all the freedom you need. You can indent with 1
space, or 56 spaces if that strikes your fancy. Why would you want the
freedom to indent inconsistently?

Sateesh> Is there any plausible reason behind this?

You should search the newsgroup archives (in google), there are
probably megabytes of discussion on this matter.
 
G

Grant Edwards

I am a beginner in Python, and am wondering what is it about
the indentation in Python, without which python scripts do not
work properly. Why can't the indentation not so strict so as
to give better freedom to the user? Is there any plausible
reason behind this?

Yes. It's about readability.

In many languages, such as C or Perl, you can write readable code, but
you can also squeeze your code in as few lines as possible, resulting in
hard to read, hard to maintain, hard to debug code[1]. Whenever I
translate a Perl script into Python, I end up with about 25-40% more
lines, but the script is much more readable than the Perl counterpart
(mostly, of course, because of the lacking $'s ;).

Compare C and Python:

In C, we have 10 lines

if (condition)
{
doThis();
doThat();
}
else
{
doWhatever();
andSoOn();
}

Which translates into 6 lines of Python:

if condition:
doThis()
doThat()
else:
doWhatever()
andSoOn()

Many fewer lines of space are wasted on delimiters. That means
you're more likely to have the entire logical "block" on the
screen at once. That means fewer bugs.

[Not to mention it takes about 1/4 to 1/3 of the actual lines
of code to accomplish the same amount of work.]
 
G

Grant Edwards

I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code. Also
indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

All languages are restrictive. C requires blocks are delimited
by {} and statements by ;. Python isn't more restrictive, it
just has _different_ restrictions.
 
P

Peter Hansen

Ville said:
Sateesh> Why can't the indentation not so strict so as to give
Sateesh> better freedom to the user?

You already have all the freedom you need. You can indent with 1
space, or 56 spaces if that strikes your fancy. Why would you want the
freedom to indent inconsistently?

Oh, believe me, some people do want that (though, I hope, not
Sateesh... he sounds more sensible :). I once had a fellow
working for me who, as it turned out, would indent effectively
arbitrary amounts each time he started a new block. We
were so astounded that several of us clustered around him to
watch him do it. He was quite amenable to the audience, too,
as he saw nothing wrong with it.

He would type (this was in C), say, an 'if' statement and its
expression. Then he would hit Enter and hold down the space
bar for a second or two. Generally he'd end up at least five
or ten spaces past the previous line's indentation. Sometimes
it was 25 spaces. The blocks for the 'if' and the 'else'
would rarely align, and if they did it was mere serendipity.

As it shortly turned out, the fellow was an iceberg, and
once we discovered the other 90% lurking below the surface,
and the seriously negative effect it had on his code (which
was all but unreadable to the rest of us, not to mention
just plain scary bad), we ditched him pretty quick.

The ironic thing is that he could just as easily have done
this with Python -- he was at least consistent within a
block...

-Peter
 
N

Nikola Plejic

Sateesh said:
I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code. Also
indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

Sateesh

Huh, then C also limits you in a way... You have to constantly open and
close those curly braces. I'd be much better if I wouldn't have to do
that, but I have to get along with it (N.B. - I'm not a C programmer so
don't get me wrong :)).

Instead of { and } you have indentation... IMHO, that's a much more
"natural" way of defining blocks of code. You'll see, you'll get used to
it. It often results in writing *much* cleaner code in other languages,
too. At least in my case.
 
P

Peter Maas

Sateesh said:
I surely understand the importance of indentation, but as you see there are
beautification tools that can be used to beautiy the C code. Also
indentation is based upon ones style of coding, and I feel somewhat
restricted when the language itself restricts someone to code in a
particular way.

You are only restricted if you want leading whitespace independent
from the block structure of your code. Formatting discussions among
C-Programmers concentrate on these topics:

1. How deep to indent:
this can be freely chosen in Python as well.

2. How to set brackets:
This issue is resolved automatically in Python. :)

Mit freundlichen Gruessen,

Peter Maas
 
S

Steve Holden

Sateesh said:
I think I would be able to better appreciate Python after I go ahead with
some programming first.
A profound conclusion, that has taken many others much longer to arrive at!
Thanks for the insights!

Sateesh

regards
Steve
 
J

Jacek Generowicz

Sateesh said:
Hi,
I am a beginner in Python, and am wondering what is it about the indentation
in Python, without which python scripts do not work properly.
Why can't the indentation not so strict so as to give better freedom to the
user?
Is there any plausible reason behind this?

def ask(lang, noise):
print """I am a beginner in %s, and am wondering what is it about the %s in
%s, without which %s programs do not work properly.
Why can't the %s not so strict so as to give better freedom to
the user?
Is there any plausible reason behind this?""" % (lang, noise, lang, lang, noise)
print

for l,n in [['C', '{,} and ;'],
['C++', '{,} and ;'],
['Java', '{,} and ;'],
['Pascal', 'begin and end'],
['Lisp', '( and )'],
['Perl', '!@#*$&!@$#']]:
ask(l,n)

keywords: parody, irony
 
R

Reinhold Birkenfeld

Grant said:
I am a beginner in Python, and am wondering what is it about
the indentation in Python, without which python scripts do not
work properly. Why can't the indentation not so strict so as
to give better freedom to the user? Is there any plausible
reason behind this?

Yes. It's about readability.

In many languages, such as C or Perl, you can write readable code, but
you can also squeeze your code in as few lines as possible, resulting in
hard to read, hard to maintain, hard to debug code[1]. Whenever I
translate a Perl script into Python, I end up with about 25-40% more
lines, but the script is much more readable than the Perl counterpart
(mostly, of course, because of the lacking $'s ;).

Compare C and Python:

In C, we have 10 lines

if (condition)
{
doThis();
doThat();
}
else
{
doWhatever();
andSoOn();
}

Well, one could apply another coding style in this example:

if (condition) {
doThis();
doThat();
} else {
doWhatever();
andSoOn();
}

which only takes 7 lines and is not much less readable. But I agree with
you!
Which translates into 6 lines of Python:

if condition:
doThis()
doThat()
else:
doWhatever()
andSoOn()

Many fewer lines of space are wasted on delimiters. That means
you're more likely to have the entire logical "block" on the
screen at once. That means fewer bugs.

[Not to mention it takes about 1/4 to 1/3 of the actual lines
of code to accomplish the same amount of work.]

ACK.

Reinhold
 
R

Reinhold Birkenfeld

Peter said:
As it shortly turned out, the fellow was an iceberg, and
once we discovered the other 90% lurking below the surface,
and the seriously negative effect it had on his code (which
was all but unreadable to the rest of us, not to mention

You mean "all but readable"?
just plain scary bad), we ditched him pretty quick.

Reinhold
 
G

Grant Edwards

Well, one could apply another coding style in this example:

if (condition) {
doThis();
doThat();
} else {
doWhatever();
andSoOn();
}

which only takes 7 lines and is not much less readable.

Maybe for some people, but I've never been able to read C code
like that. I always have to run it through 'indent' to put the
braces on separate lines. When working on code that requires
'old-style' indentation, the process is:

1) run it through indent so I can read it
2) edit/compile/test
3) run it through indent again to restore old indentation
But I agree with you!

:)
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,835
Latest member
lila30

Latest Threads

Top