structure in Python

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

    #16
    Re: Trailing comma (was: Re: structure in Python)

    Skip Montanaro wrote:
    ...[color=blue]
    > Also, if the list is not a list of string constants there's no real harm
    > in omitting it either, because if you extend or reorder the list and
    > forget to insert a comma in the right place you'll get a SyntaxError the[/color]

    Unfortunately there are lots of cases in which juxtaposing two
    would-be items gives different results than separating them with a
    comma -- e.g. i could have tuples with callable and args:
    (
    f,
    (),
    g,
    (),
    h
    (),
    k,
    ()
    )

    oops, forgot comma after h, so h is called then and there rather
    than stashed in the tuple for later processing. Yeah, eventually
    you'll probably get an error, but tracking it down will be useless work.

    Number literals are another example, if the one before which
    you forgot the comma had a sign.

    And what about lists?
    [
    [1],
    [0],
    [2]
    [0],
    [3],
    [1]
    ]

    Oops, guess what, the third "sub-list" isn't because the fourth sublist
    became an index into it instead -- darn, a missing comma.

    It's NOT just string literals, alas.

    DO use the "redundant" final comma, and live a happier life:-).


    Alex

    Comment

    • Timo Virkkala

      #17
      Re: Trailing comma

      Georgy Pruss wrote:[color=blue]
      > "Timo Virkkala" <a@a.invalid> wrote in message news:bn415p$u9s $1@nyytiset.pp. htv.fi...
      > Another "solution" is just to look at the previous line and check if it has the comma
      > or not. You'd better get a habit to THINK and not do things mechanically, as others
      > suggest.[/color]

      Yeah, that's what I have done thus far. =)

      I don't know.. I can't recall ever being burned by forgetting a comma..
      Whereas I do remember a couple of occasions where I've included a
      trailing comma where I shouldn't have (SQL).

      --
      Timo Virkkala

      Comment

      • Dennis Lee Bieber

        #18
        Re: Trailing comma (was: Re: structure in Python)

        Cousin Stanley fed this fish to the penguins on Tuesday 21 October 2003
        08:23 am:
        [color=blue]
        > This probably stems from the fact
        > that my elementary school grammar teacher
        > would whack me severely about the head and shoulders
        > if I wrote ....
        >
        > Uncle Scrooge took Huey, Duey, and Louie,
        > to the park.
        >[/color]

        Depending on the style guide the teacher is using, even "Uncle Scrooge
        took Huey, Duey, and Louie to the park" could get marked down... Though
        let me state that I've never been comfortable with "Uncle Scrooge took
        Huey, Duey and Louie to the park"... I read that as two entities --
        Huey is entity one, and some composite "Duey and Louie" is the other
        entity. (Hmmm, don't recall the comics, is it Duey or Dewey?)

        --[color=blue]
        > =============== =============== =============== =============== == <
        > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
        > wulfraed@dm.net | Bestiaria Support Staff <
        > =============== =============== =============== =============== == <
        > Bestiaria Home Page: http://www.beastie.dm.net/ <
        > Home Page: http://www.dm.net/~wulfraed/ <[/color]

        Comment

        • Christopher Koppler

          #19
          Re: Trailing comma

          Timo Virkkala <a@a.invalid> wrote in message news:<bn59s9$t9 5$1@nyytiset.pp .htv.fi>...[color=blue]
          > Georgy Pruss wrote:[color=green]
          > > "Timo Virkkala" <a@a.invalid> wrote in message news:bn415p$u9s $1@nyytiset.pp. htv.fi...
          > > Another "solution" is just to look at the previous line and check if it has the comma
          > > or not. You'd better get a habit to THINK and not do things mechanically, as others
          > > suggest.[/color]
          >
          > Yeah, that's what I have done thus far. =)
          >
          > I don't know.. I can't recall ever being burned by forgetting a comma..
          > Whereas I do remember a couple of occasions where I've included a
          > trailing comma where I shouldn't have (SQL).[/color]

          In SQL statements this seems to be common practice:

          select firstname
          ,lastname
          ,address
          from ...

          to avoid getting hurt...

          --
          Christopher

          Comment

          • Cousin Stanley

            #20
            Re: Trailing comma (was: Re: structure in Python)

            Skip ...

            Thanks for the info ....

            In the case where an addition is made to the end
            of a L O N G sequence and then followed by a resort,
            I can see that depending on how the sequence is used
            that problems arising from inadvertent omission of
            the comma delimeter by whomever extended the sequence
            might not manifest themselves immediately and could
            perhaps be difficult to spot ....

            In spite of my Python teacher's praise for using this style,
            I would probably still wonder how he/she managed to make it
            through school without getting whacked for using it themselves ....

            --
            Cousin Stanley
            Human Being
            Phoenix, Arizona

            Comment

            • Cousin Stanley

              #21
              Re: Trailing comma (was: Re: structure in Python)

              Peter ....

              Thanks for the reply ....

              I usually format long lists as one item per line as well ....

              I only stuck Alex' example on a single line
              since it was short and I was using the interpreter ....

              The style that looks the most natural and seems the easiest
              to extend and edit to me is ....

              x = [ 'fe' ,
              'fi' ,
              'fo' ,
              'fum' ]

              --
              Cousin Stanley
              Human Being
              Phoenix, Arizona

              Comment

              • Cousin Stanley

                #22
                Re: Trailing comma (was: Re: structure in Python)

                | Depending on the style guide the teacher is using,
                | even "Uncle Scrooge took Huey, Duey, and Louie to the park"
                | .... "Uncle Scrooge took Huey, Duey and Louie to the park"
                | ....

                Dennis ....

                Thanks for the reply ....

                This style difference also occurred to me
                after I posted the example ....

                I think I remember it being taught
                that it is a grammatical option
                and correct in either case ....

                I always include the _final_ comma in these cases myself,
                as it usually seems harder to read and potentially confusing
                without it ....

                --
                Cousin Stanley
                Human Being
                Phoenix, Arizona

                Comment

                • Michael Geary

                  #23
                  Re: Trailing comma

                  > > Timo Virkkala wrote:[color=blue][color=green]
                  > > Maybe I should get into the habit of always including the
                  > > trailing comma and see where I get. Before this I didn't
                  > > even know that it was allowed.[/color][/color]
                  [color=blue]
                  > Georgy Pruss wrote:
                  > Another "solution" is just to look at the previous line and
                  > check if it has the comma or not. You'd better get a habit to
                  > THINK and not do things mechanically, as others suggest.[/color]

                  It is better to think and not do things mechanically, and that's exactly why
                  people add the trailing comma.

                  If you do things mechanically, you may leave out the trailing comma, because
                  it certainly isn't necessary.

                  If you think about making your code maintainable, you will look for ways to
                  prevent mistakes when people edit your code later. The trailing comma helps
                  prevent mistakes, so you'll use it.

                  -Mike


                  Comment

                  • Peter Hansen

                    #24
                    Re: Trailing comma

                    Michael Geary wrote:[color=blue]
                    >[color=green][color=darkred]
                    > > > Timo Virkkala wrote:
                    > > > Maybe I should get into the habit of always including the
                    > > > trailing comma and see where I get. Before this I didn't
                    > > > even know that it was allowed.[/color][/color]
                    >[color=green]
                    > > Georgy Pruss wrote:
                    > > Another "solution" is just to look at the previous line and
                    > > check if it has the comma or not. You'd better get a habit to
                    > > THINK and not do things mechanically, as others suggest.[/color]
                    >
                    > It is better to think and not do things mechanically, and that's exactly why
                    > people add the trailing comma.
                    >
                    > If you do things mechanically, you may leave out the trailing comma, because
                    > it certainly isn't necessary.[/color]

                    While I agree with your point, which is that putting *in* the trailing
                    comma is the result of a great deal of thought in advance (such as we're
                    doing right now), I have to disagree with your explanation for what
                    "mechanical " would imply.

                    Mechanical would be more appropriately used when you do the same thing
                    over and over, completely without having to think. The mere fact that
                    you say "because it isn't necessary" shows that thought had to be involved
                    (to evaluate the need for the comma) and therefore it's not mechanical.
                    [color=blue]
                    > If you think about making your code maintainable, you will look for ways to
                    > prevent mistakes when people edit your code later. The trailing comma helps
                    > prevent mistakes, so you'll use it.[/color]

                    Quite true!

                    Comment

                    • Patrick Maupin

                      #25
                      Re: Trailing comma (was: Re: structure in Python)

                      Cousin Stanley wrote:
                      [color=blue]
                      > The style that looks the most natural and seems the easiest
                      > to extend and edit to me is ....
                      >
                      > x = [ 'fe' ,
                      > 'fi' ,
                      > 'fo' ,
                      > 'fum' ][/color]

                      Other people have raised some good points as to why using
                      trailing commas can be better, but for my money the best
                      reason to consistently use trailing commas is how a CVS
                      (or other) diff will look later. You can either see
                      one deleted line plus two added lines, or a single added
                      line, your choice.

                      Pat

                      Comment

                      • Georgy Pruss

                        #26
                        Re: Trailing comma (was: Re: structure in Python)


                        "Patrick Maupin" <pmaupin@speake asy.net> wrote in message news:653b7547.0 310231853.21518 9da@posting.goo gle.com...[color=blue]
                        > Cousin Stanley wrote:
                        >[color=green]
                        > > The style that looks the most natural and seems the easiest
                        > > to extend and edit to me is ....
                        > >
                        > > x = [ 'fe' ,
                        > > 'fi' ,
                        > > 'fo' ,
                        > > 'fum' ][/color]
                        >
                        > Other people have raised some good points as to why using
                        > trailing commas can be better, but for my money the best
                        > reason to consistently use trailing commas is how a CVS
                        > (or other) diff will look later. You can either see
                        > one deleted line plus two added lines, or a single added
                        > line, your choice.
                        >
                        > Pat[/color]

                        You can always find a better diff which will show you an added
                        comma and an additional line. Again, it's a tool problem.

                        Georgy


                        Comment

                        • Patrick Maupin

                          #27
                          Re: Trailing comma (was: Re: structure in Python)

                          Georgy Pruss wrote:
                          [color=blue]
                          > You can always find a better diff which will show you an added
                          > comma and an additional line. Again, it's a tool problem.[/color]

                          Do not presume, simply because I choose to arrange my affairs
                          such that I can usually use a BB gun more effectively than many
                          can use a howitzer, that I have a "tool problem."

                          Pat

                          Comment

                          • Cousin Stanley

                            #28
                            Re: Trailing comma (was: Re: structure in Python)

                            | ...
                            | the best reason to consistently use trailing commas
                            | is how a CVS (or other) diff will look later.
                            | You can either see one deleted line plus two added lines,
                            | or a single added line, your choice.

                            Patrick ....

                            Thanks for the info ....

                            I'm not currently using versioning tools
                            as I'm only a single developer and most
                            of my code at this point is very simple
                            and a backup copy or two usally work OK for me ....

                            However, I will keep this in mind for the future ....

                            --
                            Cousin Stanley
                            Human Being
                            Phoenix, Arizona

                            Comment

                            Working...