Re: Python indentation

From: Reinhold Birkenfeld (reinhold-birkenfeld-nospam_at_wolke7.net)
Date: 07/07/04


Date: Wed, 07 Jul 2004 13:18:46 +0200

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


Relevant Pages

  • Re: What is good about Prothon?
    ... end script ... provides the foundation for library-based design. ... that it takes Python several specialised and largely ... though I can understand why more static languages might require ...
    (comp.lang.python)
  • Re: indentation preservation/restoration
    ... Python code is fragile in this regard. ... One solution that occurs to me is that a "indentation code" could be appended to a script which could be used to rebuild the script if necessary. ... So go to see the source code of the HTML page in your editor and you will get the lost spaces. ...
    (comp.lang.python)
  • Re: Grouping code by indentation - feature or ******?
    ... In Python, you can eyeball ... Not so with other languages. ... (indentation), and the way. ... the flow of just about any code. ...
    (comp.lang.python)
  • Re: indentation preservation/restoration
    ... Python code is fragile in this regard. ... > solution that occurs to me is that a "indentation code" could be ... > appended to a script which could be used to rebuild the script if ... I accidentally deleted all the numeric literals from my ...
    (comp.lang.python)
  • Re: Python indentation
    ... >> the indentation in Python, without which python scripts do not ... Why can't the indentation not so strict so as ... > translate a Perl script into Python, I end up with about 25-40% more ... but the script is much more readable than the Perl counterpart ...
    (comp.lang.python)