Python indentation

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Wed Jul 7 07:18:46 EDT 2004


Sateesh wrote:
> 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*

-- 
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich.  Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
  -- David Kastrup in de.comp.os.unix.linux.misc



More information about the Python-list mailing list