Python indentation

Reinhold Birkenfeld reinhold-birkenfeld-nospam at wolke7.net
Wed Jul 7 18:47:20 CEST 2004


Grant Edwards wrote:
> On 2004-07-07, Reinhold Birkenfeld <reinhold-birkenfeld-nospam at wolke7.net> wrote:
> 
>>> 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

-- 
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