Python indentation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sateesh

    Python indentation

    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


  • Reinhold Birkenfeld

    #2
    Re: Python indentation

    Sateesh wrote:[color=blue]
    > 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?[/color]

    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 ununderstandabl e code in
    Python... *grin*

    --
    Wenn eine Linuxdistributi on 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

    Comment

    • Brian Quinlan

      #3
      Re: Python indentation

      Sateesh wrote:[color=blue]
      > Why can't the indentation not so strict so as to give better freedom to the
      > user?[/color]

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

      Cheers,
      Brian

      Comment

      • Sateesh

        #4
        Re: Python indentation

        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

        "Brian Quinlan" <brian@sweetapp .com> wrote in message
        news:mailman.61 .1089200252.513 5.python-list@python.org ...[color=blue]
        > Sateesh wrote:[color=green]
        > > Why can't the indentation not so strict so as to give better freedom to[/color][/color]
        the[color=blue][color=green]
        > > user?[/color]
        >
        > Can you specify what desirable construction Python is denying you? Or
        > are you talking about freedom in the abstract?
        >
        > Cheers,
        > Brian[/color]


        Comment

        • Brian Quinlan

          #5
          Re: Python indentation

          Sateesh wrote:[color=blue]
          > I surely understand the importance of indentation, but as you see there are
          > beautification tools that can be used to beautiy the C code.[/color]

          I'm not sure why this is relevant.
          [color=blue]
          > 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.[/color]

          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

          Comment

          • Jeff Epler

            #6
            Re: Python indentation

            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+SlJd0 1MZaTXX0RAgKgAJ 9JwwpYkjy5DIGU6 p5QfK0m7/0lowCfeyDE
            ncX4vs1DCCyfgHW 5GbCnUMk=
            =y1+2
            -----END PGP SIGNATURE-----

            Comment

            • Reinhold Birkenfeld

              #7
              Re: Python indentation

              Sateesh wrote:[color=blue]
              > 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.[/color]

              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

              --
              Wenn eine Linuxdistributi on 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

              Comment

              • Brian Elmegaard

                #8
                Re: Python indentation

                "Sateesh" <ext-sateesh.kavuri@ nokia.com> writes:
                [color=blue]
                > 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.[/color]

                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.

                --
                Brian (remove the sport for mail)

                Comment

                • Sateesh

                  #9
                  Re: Python indentation

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

                  Thanks for the insights!

                  Sateesh
                  "Brian Quinlan" <brian@sweetapp .com> wrote in message
                  news:mailman.62 .1089201328.513 5.python-list@python.org ...[color=blue]
                  > Sateesh wrote:[color=green]
                  > > I surely understand the importance of indentation, but as you see there[/color][/color]
                  are[color=blue][color=green]
                  > > beautification tools that can be used to beautiy the C code.[/color]
                  >
                  > I'm not sure why this is relevant.
                  >[color=green]
                  > > 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.[/color]
                  >
                  > 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[/color]


                  Comment

                  • Ville Vainio

                    #10
                    Re: Python indentation

                    >>>>> "Sateesh" == Sateesh <ext-sateesh.kavuri@ nokia.com> writes:

                    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.

                    --
                    Ville Vainio http://tinyurl.com/2prnb

                    Comment

                    • Grant Edwards

                      #11
                      Re: Python indentation

                      On 2004-07-07, Reinhold Birkenfeld <reinhold-birkenfeld-nospam@wolke7.n et> wrote:
                      [color=blue][color=green]
                      >> 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?[/color]
                      >
                      > 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 ;).[/color]

                      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

                      Comment

                      • Grant Edwards

                        #12
                        Re: Python indentation

                        On 2004-07-07, Sateesh <ext-sateesh.kavuri@ nokia.com> wrote:
                        [color=blue]
                        > 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.[/color]

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

                        --
                        Grant Edwards grante Yow! What I want to find
                        at out is -- do parrots know
                        visi.com much about Astro-Turf?

                        Comment

                        • Peter Hansen

                          #13
                          Re: Python indentation

                          Ville Vainio wrote:
                          [color=blue][color=green][color=darkred]
                          >>>>>>"Satees h" == Sateesh <ext-sateesh.kavuri@ nokia.com> writes:[/color][/color]
                          > 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?[/color]

                          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

                          Comment

                          • Nikola Plejic

                            #14
                            Re: Python indentation

                            Sateesh wrote:[color=blue]
                            > 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
                            >[/color]

                            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.

                            --
                            regards, [ mailto: zweistein at ftml dot net ]
                            Nikola [ http://hades.indivia.net/ ]

                            Comment

                            • Peter Maas

                              #15
                              Re: Python indentation

                              Sateesh schrieb:[color=blue]
                              > 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.[/color]

                              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

                              --
                              -------------------------------------------------------------------
                              Peter Maas, M+R Infosysteme, D-52070 Aachen, Tel +49-241-93878-0
                              E-mail 'cGV0ZXIubWFhc0 BtcGx1c3IuZGU=\ n'.decode('base 64')
                              -------------------------------------------------------------------

                              Comment

                              Working...