indendation question

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

    indendation question

    Hi all,

    What is the standard and recommended way of indendation one should get to use in python programming ? is it a tab or 2 spaces or 4 spaces ? i am confused. kindly enlighten

    thanks,
    KM


  • Terry Reedy

    #2
    Re: indendation question


    "km" <km@mrna.tn.nic .in> wrote in message
    news:mailman.11 5.1072408950.68 4.python-list@python.org ...[color=blue]
    > Hi all,
    >
    > What is the standard and recommended way of indendation one should get to[/color]
    use in python programming ? is it a tab or 2 spaces or 4 spaces ? i am
    confused. kindly enlighten

    The recommendation in the early numbered PEP on style is 4 spaces. This is
    the standard for the standard library.

    TJR


    Comment

    • Dan Bishop

      #3
      Re: indendation question

      km <km@mrna.tn.nic .in> wrote in message news:<mailman.1 15.1072408950.6 84.python-list@python.org >...[color=blue]
      > Hi all,
      >
      > What is the standard and recommended way of indendation one should get to use
      > in python programming? is it a tab or 2 spaces or 4 spaces? i am confused.
      > kindly enlighten[/color]

      It doesn't matter how much indentation you use, as long as you're
      consistent. But it is recommended to never use tabs, and *strongly*
      recommended to never mix tabs and spaces.

      I usually use 3 spaces. Most of the regulars on this newsgroup use 4.

      Comment

      • Stephen Ferg

        #4
        Re: indendation question

        The official recommendation is to use 4 spaces, but...

        .... Using tabs seems much more natural to me.

        One level of indentation == one tab character.
        What could be more natural?

        So I always use tabs, and that's what I recommend.

        Comment

        • Robin Munn

          #5
          Re: indendation question

          Stephen Ferg <steve@ferg.org > wrote:[color=blue]
          > The official recommendation is to use 4 spaces, but...
          >
          > ... Using tabs seems much more natural to me.
          >
          > One level of indentation == one tab character.
          > What could be more natural?
          >
          > So I always use tabs, and that's what I recommend.[/color]

          The problem with tab characters is that it makes it very difficult to
          share your code with others. Everyone has different settings for their
          tab stops: some use 4, most use 8, a few heretics :-) use 3... Thus,
          your code, that you carefully arranged to be less than 80 characters
          wide, will someday be read by someone who has 8-space tabs (while you
          use 4-space tabs), and on his screen, it will wrap around most
          annoyingly.

          And if someone else is *editing* the same file as you, it will make
          matters even worse, because they're likely to be using spaces for
          indentation. So you've indented something with one tab, which on their
          screen looks like 8 spaces. They add a line at what they believe to be
          the same level of indentation: 8 spaces. Then you look at the file and
          see their line as being one step more indented than yours. Now try to
          guess what Python will do with that file.

          This isn't just a theoretical problem: I've personally cleaned up files
          (although they weren't written in Python) that had had exactly this
          happen to them. That's why I recommend space characters, not tabs. Yes,
          it may feel more natural to just hit the Tab key to indent -- but any
          decent programming editor can be set up to insert the right number of
          spaces when you hit the Tab key! In vim, for instance, look at the
          "shiftwidth " and "smarttab" options.

          On the other hand, read the rationale for PEP 666:

          Everybody agrees that mixing tabs and spaces is a bad idea. Some people want more than this. I propose that we let people define whatever Python behaviour they want, so it will only run the way they like it, and will not run the way they don’t like it...


          --
          Robin Munn
          rmunn@pobox.com

          Comment

          • Jarek Zgoda

            #6
            Re: indendation question

            Robin Munn <rmunn@pobox.co m> pisze:
            [color=blue]
            > And if someone else is *editing* the same file as you, it will make
            > matters even worse, because they're likely to be using spaces for
            > indentation. So you've indented something with one tab, which on their
            > screen looks like 8 spaces. They add a line at what they believe to be
            > the same level of indentation: 8 spaces. Then you look at the file and
            > see their line as being one step more indented than yours. Now try to
            > guess what Python will do with that file.[/color]

            That's why I have set all my editors (UltraEdit, Vim, Kate) to convert
            tabs to spaces (1:4) on opening any file. Sometimes this leads to
            intendation errors on startup, but having only spaces in files this
            error is easy to trace.

            --
            Jarek Zgoda
            Unregistered Linux User #-1
            http://www.zgoda.biz/ JID:zgoda-a-chrome.pl http://zgoda.jogger.pl/

            Comment

            Working...