FAQ
Hello,


At the end of the last line of the following program,
there is a comma, I dont understand why ?


Thx




from cx_Freeze import setup, Executable


# On appelle la fonction setup
setup(
     name = "salut",
     version = "0.1",
     description = "Ce programme vous dit bonjour",
     executables = [Executable("salut.py")], # <--- HERE
)

Search Discussions

  • Ast at Sep 3, 2015 at 12:28 pm
    "ast" <nomail@invalid.com> a ?crit dans le message de news:55e83afb$0$3157$426a74cc at news.free.fr...
    Hello,
    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )

    Ok its understood, it's a 1 element only tuple


    example:

    A = 5,
    A
    (5,)

    A = (6)
    A
    6
  • Martin Komoň at Sep 3, 2015 at 12:34 pm
    In this case those are not tuples but rather arguments in a function
    call. The extra comma does not change the evaluation, my guess is that
    it is there for easier adding/removing arguments without having to care
    about trailing commas.


    Martin

    On 03/09/15 14:28, ast wrote:

    "ast" <nomail@invalid.com> a ?crit dans le message de
    news:55e83afb$0$3157$426a74cc at news.free.fr...
    Hello,
    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )
    Ok its understood, it's a 1 element only tuple

    example:
    A = 5,
    A
    (5,)
    A = (6)
    A
    6
  • Peter Otten at Sep 3, 2015 at 12:48 pm
    ast wrote:

    "ast" <nomail@invalid.com> a ?crit dans le message de
    news:55e83afb$0$3157$426a74cc at news.free.fr...
    Hello,
    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )
    Ok its understood, it's a 1 element only tuple

    example:
    A = 5,
    A
    (5,)
    A = (6)
    A
    6

    No, in a function call an extra comma has no effect:

    def f(x): return x
    ...
    f(42)
    42
    f(42,)
    42
    f(xB)
    42
    f(xB,)
    42


    The only reason I see to add an extra comma are smaller and easier to read
    diffs when you make a change:


    $ cat before.py
    func(
         arg_one=1,
         arg_two=2
    )


    func(
         arg_one=1,
         arg_two=2,
    )
    $ cat after.py
    func(
         arg_one=1,
         arg_two=2,
         arg_three=3
    )


    func(
         arg_one=1,
         arg_two=2,
         arg_three=3,
    )
    $ diff -u before.py after.py
    --- before.py 2015-09-03 14:44:27.709735075 +0200
    +++ after.py 2015-09-03 14:44:55.275958331 +0200
    @@ -1,9 +1,11 @@
      func(
          arg_one=1,
    - arg_two=2
    + arg_two=2,
    + arg_three=3
      )


      func(
          arg_one=1,
          arg_two=2,
    + arg_three=3,
      )
  • Tim Chase at Sep 3, 2015 at 2:36 pm

    On 2015-09-03 14:48, Peter Otten wrote:
    The only reason I see to add an extra comma are smaller and easier
    to read diffs when you make a change:

    While that's the primary reason I do it, it's also helpful if you
    have a bunch of named keyword arguments and want sort/rearrange them
    (usually for clarity/grouping). You don't have to worry about finding
    the previous-last-item and adding a comma to it and then finding the
    new-last-item and removing its comma. Also, when adding a new
    item, you can just copy an existing line, paste it, and modify the
    salient parts without needing to append a comma to one line or
    delete it from the pasted line.


    But the improvement in diff output? That's a big win for me.


    I notice it most when I *can't* use it, like in writing SQL:


       SELECT
         col1,
         col2,
         col3, -- grr, can't do this
       FROM tblExample


    so my SQL diffs are the "removed this line and replaced it with
    something almost identical except it now has a comma". Harumph.


    -tkc
  • Nick Sarbicki at Sep 3, 2015 at 3:51 pm
    Tim,


    Doesn't work for the first column in SQL, but we tend to put the comma and
    a space before the column name. It makes it easier to move things around
    and (debateably) more readable. It is also very obvious when you have
    missed a comma this way.


    - Nick


    On Thu, 3 Sep 2015 16:14 Tim Chase wrote:

    On 2015-09-03 14:48, Peter Otten wrote:
    The only reason I see to add an extra comma are smaller and easier
    to read diffs when you make a change:
    While that's the primary reason I do it, it's also helpful if you
    have a bunch of named keyword arguments and want sort/rearrange them
    (usually for clarity/grouping). You don't have to worry about finding
    the previous-last-item and adding a comma to it and then finding the
    new-last-item and removing its comma. Also, when adding a new
    item, you can just copy an existing line, paste it, and modify the
    salient parts without needing to append a comma to one line or
    delete it from the pasted line.

    But the improvement in diff output? That's a big win for me.

    I notice it most when I *can't* use it, like in writing SQL:

    SELECT
    col1,
    col2,
    col3, -- grr, can't do this
    FROM tblExample

    so my SQL diffs are the "removed this line and replaced it with
    something almost identical except it now has a comma". Harumph.

    -tkc


    --
    https://mail.python.org/mailman/listinfo/python-list
    --
      - Nick
    -------------- next part --------------
    An HTML attachment was scrubbed...
    URL: <http://mail.python.org/pipermail/python-list/attachments/20150903/a70653b9/attachment.html>
  • MRAB at Sep 3, 2015 at 12:50 pm

    On 2015-09-03 13:28, ast wrote:
    "ast" <nomail@invalid.com> a ?crit dans le message de news:55e83afb$0$3157$426a74cc at news.free.fr...
    Hello,
    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )
    Ok its understood, it's a 1 element only tuple

    example:
    A = 5,
    A
    (5,)
    A = (6)
    A
    6
    No, it's not a tuple, because it's part of the argument list of 'setup'.


    A trailing comma is allowed in argument lists, tuples, lists, etc.

    (1, 2, )
    (1, 2)
    [1, 2, ]
    [1, 2]
    {1, 2, }
    {1, 2}
    print(1, 2, )
    1 2
    {'1': 'one', '2': 'two', }
    {'2': 'two', '1': 'one'}


    It's nice to be able to do that because if you write the items on
    separate lines, like in your example, it's simpler when editing: all of
    the lines can end with a comma; if you add a new line, you don't also
    have to add a comma to the end of the previous line (a new line is
    added, and that's that); when removing a line, you don't also have to
    remove the comma from the end of the previous line (an old line is
    removed, and that's that).
  • Vladimir Ignatov at Sep 3, 2015 at 12:50 pm

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )
    Ok its understood, it's a 1 element only tuple

    example:
    A = 5,
    A
    (5,)
    A = (6)
    A
    6

    No. It's not a tuple in your case (calling 'setup' function)


    a = 1,2, # <- extra comma
    print a
    b = 1, # <- extra comma
    print b


    =>


    (1, 2) # ignored
    (1,) # made tuple


    and


    def f(a, b):
         print a,b


    f(1,2,) # <- extra comma


    =>


    1 2 # ignored


    Under some circumstances python ignores "excess" comma. At least
    inside list definition [1,2,3,] and function calls f(1,2,)


    Vladimir


    http://itunes.apple.com/us/app/python-code-samples/id1025613117
  • Laura Creighton at Sep 3, 2015 at 12:40 pm

    In a message of Thu, 03 Sep 2015 14:20:06 +0200, "ast" writes:
    Hello,

    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )

    In python a tuple consists of a number of values separated by commas.
    see: https://docs.python.org/3/tutorial/datastructures.html#tuples-and-sequences
    or https://docs.python.org/2/tutorial/datastructures.html#tuples-and-sequences
    for Python 2.


    The round parentheses aren't significant.


    So:

    def Executable(arg):
    ... return arg
    ...
    executables = [Executable("salut.py")],
    executables
      (['salut.py'],)
    >>>


    Laura
  • Laura Creighton at Sep 3, 2015 at 1:02 pm
    No, I am wrong. You are in the middle of a fuction definition.
    You are correct, that is a wierd place for a comma, though I can
    see doing that if you anticipate adding more arguments to
    the function in the near future.


    Laura
  • Ast at Sep 3, 2015 at 1:01 pm
    "ast" <nomail@invalid.com> a ?crit dans le message de news:55e83afb$0$3157$426a74cc at news.free.fr...
    Hello,
    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )

    understood
    Thx all
  • Sven R. Kunze at Sep 3, 2015 at 11:01 pm

    On 03.09.2015 14:20, ast wrote:
    Hello,
    At the end of the last line of the following program,
    there is a comma, I dont understand why ?

    Thx


    from cx_Freeze import setup, Executable

    # On appelle la fonction setup
    setup(
    name = "salut",
    version = "0.1",
    description = "Ce programme vous dit bonjour",
    executables = [Executable("salut.py")], # <--- HERE
    )

    I know of several projects having this convention because when using a
    repository software like git, it leads to smaller and thus more readable
    diffs.


    Best,
    Sven

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedSep 3, '15 at 12:20p
activeSep 3, '15 at 11:01p
posts12
users9
websitepython.org

People

Translate

site design / logo © 2019 Grokbase