Style question...

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

    Style question...

    If I end indentation levels with "pass" statements, will I piss off people
    that have to read my code? eg:

    for i in xrange(0,5):
    if i:
    print i
    pass
    print i * -1
    pass

    I ask for two reasons... a) it helps emacs figure it out, and b) I'm more
    comfortable ending compound statements with a token.


  • Peter Hansen

    #2
    Re: Style question...

    Anthony Roberts wrote:[color=blue]
    >
    > If I end indentation levels with "pass" statements, will I piss off people
    > that have to read my code? eg:
    >
    > for i in xrange(0,5):
    > if i:
    > print i
    > pass
    > print i * -1
    > pass
    >
    > I ask for two reasons... a) it helps emacs figure it out, and b) I'm more
    > comfortable ending compound statements with a token.[/color]

    Sorry, but yes, I find that ugly. If you're just starting out with
    Python, please just give it a shot without that approach for a while
    and see whether you become much more comfortable *without* the token
    than you ever were with it.

    -Peter

    Comment

    • Bruno Desthuilliers

      #3
      Re: Style question...

      Anthony Roberts wrote:[color=blue]
      > If I end indentation levels with "pass" statements, will I piss off people
      > that have to read my code? eg:
      >
      > for i in xrange(0,5):
      > if i:
      > print i
      > pass
      > print i * -1
      > pass[/color]

      *yuck* :(

      (oops, sorry...)

      Bruno

      Comment

      • Sean Ross

        #4
        Re: Style question...

        "Anthony Roberts" <anthonyr-at-hotmail-dot-com@nospam.com> wrote in message
        news:Uva3b.4943 0$la.826046@new s1.calgary.shaw .ca...[color=blue]
        > If I end indentation levels with "pass" statements, will I piss off people
        > that have to read my code? eg:
        >
        > for i in xrange(0,5):
        > if i:
        > print i
        > pass
        > print i * -1
        > pass
        >
        > I ask for two reasons... a) it helps emacs figure it out, and b) I'm more
        > comfortable ending compound statements with a token.
        >[/color]


        Hi. I tried to do something like that too, early on. (I recall being
        frustrated to find I couldn't just alias "end = pass", and use 'end' as a
        block delimiter... heh). I didn't actually want to use 'end' (I like
        significant whitespace), I just saw a lot of posts with complaints about
        "no-block-delimiter", and I thought, I wonder if you can make one. Well, you
        can (of course), but not like that.

        Anyway. I think the suggested idiom for people who must have a visible block
        delimiter is "# end <keyword>". If you use this, I think emacs can "figure
        it out" (I don't know, I don't use emacs, but I think pymode can handle
        this), and you can also use Tools/scripts/pindent.py, if you like.

        "from pindent.py"
        # This file contains a class and a main program that perform three
        # related (though complimentary) formatting operations on Python
        # programs. When called as "pindent -c", it takes a valid Python
        # program as input and outputs a version augmented with block-closing
        # comments. When called as "pindent -d", it assumes its input is a
        # Python program with block-closing comments and outputs a commentless
        # version. When called as "pindent -r" it assumes its input is a
        # Python program with block-closing comments but with its indentation
        # messed up, and outputs a properly indented version.

        [snip]

        # Secret feature:
        # - On input, a block may also be closed with an "end statement" --
        # this is a block-closing comment without the '#' sign.

        Hmm. Looks like you can also use "end", or perhaps 'end' . I don't know if
        the <keyword> is optional or not, having never used this module.
        Anyway, if you use this format, then, if someone doesn't like seeing all
        that noise when they read your code, they can strip it out with
        "pindent -d". Handy. And, when you read other peoples code, you can clutter
        it up nicely with "pindent -c". Best of both worlds, really.

        HTH
        Sean


        Comment

        • Chad Netzer

          #5
          Re: Style question...

          On Wed, 2003-08-27 at 15:45, Anthony Roberts wrote:[color=blue]
          > If I end indentation levels with "pass" statements, will I piss off people
          > that have to read my code? eg:
          >
          > for i in xrange(0,5):
          > if i:
          > print i
          > pass
          > print i * -1
          > pass[/color]

          I do this myself at times, to help emacs. But I'd suggest you not
          overdo it. It is more "pythonic" to simply leave the line blank (ie.
          vertical whitespace), than have "pass" everywhere.

          Pressing backspace once (to undo the auto-indent that emacs gives inside
          loops) is less typing than 'pass'. And if you need to reindent large
          chunks of code, you are better off using emacs block indent/dedent
          feature than relying on "pass" as defacto block delimiter (this has been
          my experience)

          Also, look into the pindent.py script (included in Python's "Tools"
          directory with the distribution), for another way of producing block
          closing tokens.

          --

          Chad Netzer


          Comment

          • Anthony Roberts

            #6
            Re: Style question...

            > Sorry, but yes, I find that ugly. If you're just starting out with[color=blue]
            > Python, please just give it a shot without that approach for a while
            > and see whether you become much more comfortable *without* the token
            > than you ever were with it.[/color]

            Well, when in Rome I guess.

            Having the closing token at the same indent level as the compound statement
            is ugly anyway. :)


            Comment

            • John Roth

              #7
              Re: Style question...


              "Anthony Roberts" <anthonyr-at-hotmail-dot-com@nospam.com> wrote in message
              news:Uva3b.4943 0$la.826046@new s1.calgary.shaw .ca...[color=blue]
              > If I end indentation levels with "pass" statements, will I piss off people
              > that have to read my code? eg:
              >
              > for i in xrange(0,5):
              > if i:
              > print i
              > pass
              > print i * -1
              > pass
              >
              > I ask for two reasons... a) it helps emacs figure it out, and b) I'm more
              > comfortable ending compound statements with a token.[/color]

              Isn't there some kind of emacs plugin, macro or mode that
              handles Python? I'm not an emacs person, but I've heard that
              there is, and I would expect that it would handle things like
              auto-indent for you.

              John Roth[color=blue]
              >
              >[/color]


              Comment

              • Anthony Roberts

                #8
                Re: Style question...

                > Isn't there some kind of emacs plugin, macro or mode that[color=blue]
                > handles Python? I'm not an emacs person, but I've heard that
                > there is, and I would expect that it would handle things like
                > auto-indent for you.[/color]

                There is, but it can't tell if I want some stuff indented or not.


                Comment

                • Anthony Roberts

                  #9
                  Re: Style question...

                  > Hi. I tried to do something like that too, early on. (I recall being[color=blue]
                  > frustrated to find I couldn't just alias "end = pass", and use 'end' as a
                  > block delimiter... heh). I didn't actually want to use 'end' (I like
                  > significant whitespace), I just saw a lot of posts with complaints about
                  > "no-block-delimiter", and I thought, I wonder if you can make one. Well,[/color]
                  you[color=blue]
                  > can (of course), but not like that.
                  >
                  > Anyway. I think the suggested idiom for people who must have a visible[/color]
                  block[color=blue]
                  > delimiter is "# end <keyword>". If you use this, I think emacs can "figure
                  > it out" (I don't know, I don't use emacs, but I think pymode can handle
                  > this), and you can also use Tools/scripts/pindent.py, if you like.[/color]

                  My emacs can't handle it... I think I'm going to subject myself to a bit
                  more of the Python way to see if I change my mind.


                  Comment

                  • Anthony Roberts

                    #10
                    Re: Style question...

                    > *yuck* :(

                    LOL!

                    I'm sorry. :)


                    Comment

                    • Erik Max Francis

                      #11
                      Re: Style question...

                      Anthony Roberts wrote:
                      [color=blue]
                      > What's the keystroke for that?[/color]

                      C-c < and C-c >.

                      --
                      Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                      __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                      / \ Nobody's on nobody's side
                      \__/ Florence, _Chess_

                      Comment

                      • Chad Netzer

                        #12
                        Re: Style question...

                        On Wed, 2003-08-27 at 19:06, Erik Max Francis wrote:[color=blue]
                        > Anthony Roberts wrote:
                        >[color=green]
                        > > There is, but it can't tell if I want some stuff indented or not.[/color]
                        >
                        > Sure it can. Hit tab or backspace.[/color]

                        I think the problem is that people learn to reindent chunks of code by
                        the following technique:

                        <arrow down>
                        <TAB>
                        <arrow down>
                        <TAB>
                        ..
                        ..
                        ..


                        This has the unfortunate consequence of breaking your code when you hit
                        multiply indented loops, nested functions, function or loop boundaries,
                        etc.

                        However, by sticking 'pass' at the end of loops, emacs's python-mode
                        will assume a dedent and preserve the indenting at boundaries.

                        But, this technique will STILL fail on multiply nested loops, etc.
                        (emacs does it's best, but it cannot mind read) So it is a terrible
                        habit to get into, because it WILL cause you problems if relied upon
                        blindly.

                        So, it is better to NOT rely on the 'pass' crutch, and instead rely on
                        the python mode operations for reindenting blocks of code (which Eric
                        provided; surely this is becoming a FAQ)

                        So, to Anthony, if you are using the TAB key to indent blocks of code,
                        and relying on 'pass' to make it easier, just be aware that this
                        technique is NOT robust.

                        --
                        Chad Netzer <cnetzer@sonic. net>


                        Comment

                        • Erik Max Francis

                          #13
                          Re: Style question...

                          Chad Netzer wrote:
                          [color=blue]
                          > But, this technique will STILL fail on multiply nested loops, etc.
                          > (emacs does it's best, but it cannot mind read) So it is a terrible
                          > habit to get into, because it WILL cause you problems if relied upon
                          > blindly.
                          >
                          > So, it is better to NOT rely on the 'pass' crutch, and instead rely on
                          > the python mode operations for reindenting blocks of code (which Eric
                          > provided; surely this is becoming a FAQ)[/color]

                          Agreed. If you're going to write code in a particular language, you
                          should fully embrace the language and its style, or move on to something
                          that you feel more comfortable with. These kind of crutches will only
                          serve to bite you later -- they aren't panaceas, and you may forget to
                          use them or be hobbled when interacting with someone else's code (where
                          you're not going to convince others to use your crutches). There's also
                          an indirect effect; if I'm reading someone else's Python code, and, say,
                          see someone using semicolons as statement delimiters on every line, I'm
                          not exactly going to be struck with a great deal of confidence.

                          --
                          Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
                          __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
                          / \ Don't ever get discouraged / There's always / A better day
                          \__/ TLC

                          Comment

                          • Anthony Roberts

                            #14
                            Re: Style question...

                            > C-c < and C-c >.

                            Thanks. :)

                            BTW: I've had your website bookmarked since 2001 because a number of the
                            things there are useful and cool.


                            Comment

                            • Anthony Roberts

                              #15
                              Re: Style question...

                              > I think the problem is that people learn to reindent chunks of code by[color=blue]
                              > the following technique:[/color]

                              Yup. In emacs, it is robust for C and C decendants. Indent region can do it
                              to entire files.

                              It's good for structural modifications when you want to change some outer
                              loop that ends up changing the indent/nesting level of some inner loop.

                              In Java, which is where I really started to lean of that feature heavily, it
                              comes with so *many* data structure classes that even though they mostly
                              share interfaces, you still come up with a cleaner way later on and go back
                              to change it. I'm finding Python is more a matter of "for x in
                              <whatever>:". ..

                              But I still change my mind sometimes. :)


                              Comment

                              Working...