Python indentation

Grant Edwards grante at visi.com
Wed Jul 7 09:06:58 EDT 2004


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();      
    }    

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

-- 
Grant Edwards                   grante             Yow!  Thank god!!... It's
                                  at               HENNY YOUNGMAN!!
                               visi.com            



More information about the Python-list mailing list