keeping Python code properly indented

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • beliavsky@aol.com

    keeping Python code properly indented

    How do you keep Python code properly indented as you modify it? I use
    an Emacs-type editor that has a Python mode, so the initial indenting
    is easy. If I later want to put a 'for' loop (or an 'if' statement)
    around a bunch of code, I find myself going through the body of the
    loop, manually re-indenting to keep the nested loops correct. There
    should be a better way.

    I think that having a 'for' loop end with a matching 'next' statement,
    as done in (for example) Basic, is a safer and clearer way of writing
    loops, although some Python programmers consider this a 'feature'
    rather than a 'bug'.
  • Thomas Heller

    #2
    Re: keeping Python code properly indented

    beliavsky@aol.c om writes:
    [color=blue]
    > How do you keep Python code properly indented as you modify it? I use
    > an Emacs-type editor that has a Python mode, so the initial indenting
    > is easy. If I later want to put a 'for' loop (or an 'if' statement)
    > around a bunch of code, I find myself going through the body of the
    > loop, manually re-indenting to keep the nested loops correct. There
    > should be a better way.
    >
    > I think that having a 'for' loop end with a matching 'next' statement,
    > as done in (for example) Basic, is a safer and clearer way of writing
    > loops, although some Python programmers consider this a 'feature'
    > rather than a 'bug'.[/color]

    In Xemacs with python-mode, I just mark the region and hit 'C-c >'
    py-shift-region-right or 'C-c <' py-shift-region-left.

    Thomas

    Comment

    • Harry George

      #3
      Re: keeping Python code properly indented

      beliavsky@aol.c om writes:
      [color=blue]
      > How do you keep Python code properly indented as you modify it? I use
      > an Emacs-type editor that has a Python mode, so the initial indenting
      > is easy. If I later want to put a 'for' loop (or an 'if' statement)
      > around a bunch of code, I find myself going through the body of the
      > loop, manually re-indenting to keep the nested loops correct. There
      > should be a better way.
      >
      > I think that having a 'for' loop end with a matching 'next' statement,
      > as done in (for example) Basic, is a safer and clearer way of writing
      > loops, although some Python programmers consider this a 'feature'
      > rather than a 'bug'.[/color]

      You do not need to "go through the body, manually re-indenting". In
      emacs, block select everything to be indented and then do the right
      shift in one step. That action is in the python-mode.el's added menu,
      and it is available as keystrokes ("C-c >" by default). Similarly for
      undenting.

      Is there a safety concern which requires solution through closure
      markers? It is a potential problem, but isn't worth closure markers
      in the language. The main solution is to not let indenting get out of
      whack in the first place:

      a) Have a rigorous newbie setup process to assure the editor does
      4-char hard spaces (no tab chars), and understands python-mode
      indenting. See: http://www.python.org/peps/pep-0008.html

      b) During development, run unittest-based regression tests every
      minute or so. A messed-up indent will either fail to compile or will
      fail a test. This enforces the indents and that becomes valued a
      language feature when doing code reviews.

      c) Immediately fix indents if problems are detected.

      d) Keep the code under revision control, so older (known good) code
      can be recovered.

      (If you can't assure a-d, then there are bigger problems than language
      features.)

      Finally, if you really think you need closure markers,
      do so with comments instead of language changes:

      if x > y:
      z=0
      #end if

      You will still be forced to get the indenting right, but at least you
      will have clues for recovering mangled indenting.

      --
      harry.g.george@ boeing.com
      6-6M31 Knowledge Management
      Phone: (425) 342-5601

      Comment

      • John Roth

        #4
        Re: keeping Python code properly indented


        <beliavsky@aol. com> wrote in message
        news:3064b51d.0 401151125.711ad e4c@posting.goo gle.com...[color=blue]
        > How do you keep Python code properly indented as you modify it? I use
        > an Emacs-type editor that has a Python mode, so the initial indenting
        > is easy. If I later want to put a 'for' loop (or an 'if' statement)
        > around a bunch of code, I find myself going through the body of the
        > loop, manually re-indenting to keep the nested loops correct. There
        > should be a better way.
        >
        > I think that having a 'for' loop end with a matching 'next' statement,
        > as done in (for example) Basic, is a safer and clearer way of writing
        > loops, although some Python programmers consider this a 'feature'
        > rather than a 'bug'.[/color]

        Just about every editor in existence allows you to select a range
        of lines and indent or dedent them as a group. If your editor
        doesn't let you do this, get a better editor.

        John Roth


        Comment

        • Jason Mobarak

          #5
          Re: keeping Python code properly indented

          The tabnanny module/program is a very good way to make sure python files
          are properly indented.

          @ares shtoom --> python -c "import tabnanny; tabnanny.check( '.')"
          ../ui/base.py 9 '\tfrom shtoom import prefs \n'
          ../test/test_stun.py 18 "\tn1 = NetAddress('10/8')\n"
          ../audio/__init__.py 21 '\taudio = attempt()\n'
          ../multicast/unixspec.py 24 '\tgroup = gethostbyname(a ddr)\n'
          ../multicast/SAP.py 8 '\tself.dismant lePacket(pack)\ n'
          ../multicast/SDP.py 35 "\tlines = text.split('\\n ')\n"
          ../multicast/netnum.py 10 '\tl = l+[0]*(4-len(l))\n'
          ../rtp.py 128 '\treturn d\n'


          beliavsky@aol.c om wrote:[color=blue]
          > How do you keep Python code properly indented as you modify it? I use
          > an Emacs-type editor that has a Python mode, so the initial indenting
          > is easy. If I later want to put a 'for' loop (or an 'if' statement)
          > around a bunch of code, I find myself going through the body of the
          > loop, manually re-indenting to keep the nested loops correct. There
          > should be a better way.
          >
          > I think that having a 'for' loop end with a matching 'next' statement,
          > as done in (for example) Basic, is a safer and clearer way of writing
          > loops, although some Python programmers consider this a 'feature'
          > rather than a 'bug'.[/color]

          --
          (------------------------------(
          )~~~~~ Jason A. Mobarak ~~~~~~~)
          (~~ aether_at_gento o_dot_org ~~(
          )~~~~ jmob_at_unm_dot _edu ~~~~~)
          (------------------------------(

          Comment

          • David Brown

            #6
            Re: keeping Python code properly indented


            <beliavsky@aol. com> wrote in message
            news:3064b51d.0 401151125.711ad e4c@posting.goo gle.com...[color=blue]
            > How do you keep Python code properly indented as you modify it? I use
            > an Emacs-type editor that has a Python mode, so the initial indenting
            > is easy. If I later want to put a 'for' loop (or an 'if' statement)
            > around a bunch of code, I find myself going through the body of the
            > loop, manually re-indenting to keep the nested loops correct. There
            > should be a better way.
            >[/color]

            Any decent editor will do the indenting in a couple of keystrokes. And even
            doing it manually, if your looped code is so many lines that manually
            "tabbing" each is a a big chore, you should probably break it into smaller
            functions for clarity.
            [color=blue]
            > I think that having a 'for' loop end with a matching 'next' statement,
            > as done in (for example) Basic, is a safer and clearer way of writing
            > loops, although some Python programmers consider this a 'feature'
            > rather than a 'bug'.[/color]

            When programming in Basic, you still have to indent the contents of the loop
            (manually or otherwise, depending on the editor). Just because the basic
            interpreter / compiler will work without sensible indentation, does not mean
            that mismatches between the code structure and the code appearance
            (especially indentation) is in any way a good thing.



            Comment

            Working...