Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #95923 > unrolled thread

Strange location for a comma

Started by"ast" <nomail@invalid.com>
First post2015-09-03 14:20 +0200
Last post2015-09-04 01:01 +0200
Articles 11 — 8 participants

Back to article view | Back to comp.lang.python


Contents

  Strange location for a comma "ast" <nomail@invalid.com> - 2015-09-03 14:20 +0200
    Re: Strange location for a comma "ast" <nomail@invalid.com> - 2015-09-03 14:28 +0200
      Re: Strange location for a comma Peter Otten <__peter__@web.de> - 2015-09-03 14:48 +0200
      Re: Strange location for a comma MRAB <python@mrabarnett.plus.com> - 2015-09-03 13:50 +0100
      Fwd: Strange location for a comma Vladimir Ignatov <kmisoft@gmail.com> - 2015-09-03 08:50 -0400
      Re: Strange location for a comma Tim Chase <python.list@tim.thechases.com> - 2015-09-03 09:36 -0500
      Re: Strange location for a comma Martin Komoň <martin@mkomon.cz> - 2015-09-03 14:34 +0200
    Re: Strange location for a comma Laura Creighton <lac@openend.se> - 2015-09-03 14:40 +0200
    Re: Strange location for a comma "ast" <nomail@invalid.com> - 2015-09-03 15:01 +0200
    Re: Strange location for a comma Laura Creighton <lac@openend.se> - 2015-09-03 15:02 +0200
    Re: Strange location for a comma "Sven R. Kunze" <srkunze@mail.de> - 2015-09-04 01:01 +0200

#95923 — Strange location for a comma

From"ast" <nomail@invalid.com>
Date2015-09-03 14:20 +0200
SubjectStrange location for a comma
Message-ID<55e83afb$0$3157$426a74cc@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
)

[toc] | [next] | [standalone]


#95924

From"ast" <nomail@invalid.com>
Date2015-09-03 14:28 +0200
Message-ID<55e83ce3$0$3327$426a74cc@news.free.fr>
In reply to#95923
"ast" <nomail@invalid.com> a écrit dans le message de news:55e83afb$0$3157$426a74cc@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

[toc] | [prev] | [next] | [standalone]


#95927

FromPeter Otten <__peter__@web.de>
Date2015-09-03 14:48 +0200
Message-ID<mailman.61.1441284505.8327.python-list@python.org>
In reply to#95924
ast wrote:

> 
> "ast" <nomail@invalid.com> a écrit dans le message de
> news:55e83afb$0$3157$426a74cc@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(x=42)
42
>>> f(x=42,)
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,
 )

[toc] | [prev] | [next] | [standalone]


#95928

FromMRAB <python@mrabarnett.plus.com>
Date2015-09-03 13:50 +0100
Message-ID<mailman.62.1441284649.8327.python-list@python.org>
In reply to#95924
On 2015-09-03 13:28, ast wrote:
>
> "ast" <nomail@invalid.com> a écrit dans le message de news:55e83afb$0$3157$426a74cc@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).

[toc] | [prev] | [next] | [standalone]


#95929

FromVladimir Ignatov <kmisoft@gmail.com>
Date2015-09-03 08:50 -0400
Message-ID<mailman.63.1441284664.8327.python-list@python.org>
In reply to#95924
>>
>> # 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

[toc] | [prev] | [next] | [standalone]


#95939

FromTim Chase <python.list@tim.thechases.com>
Date2015-09-03 09:36 -0500
Message-ID<mailman.69.1441293213.8327.python-list@python.org>
In reply to#95924
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

[toc] | [prev] | [next] | [standalone]


#95957

FromMartin Komoň <martin@mkomon.cz>
Date2015-09-03 14:34 +0200
Message-ID<mailman.86.1441297455.8327.python-list@python.org>
In reply to#95924
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@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
> 

[toc] | [prev] | [next] | [standalone]


#95926

FromLaura Creighton <lac@openend.se>
Date2015-09-03 14:40 +0200
Message-ID<mailman.60.1441284046.8327.python-list@python.org>
In reply to#95923
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

  

[toc] | [prev] | [next] | [standalone]


#95931

From"ast" <nomail@invalid.com>
Date2015-09-03 15:01 +0200
Message-ID<55e844c3$0$3356$426a74cc@news.free.fr>
In reply to#95923
"ast" <nomail@invalid.com> a écrit dans le message de news:55e83afb$0$3157$426a74cc@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 

[toc] | [prev] | [next] | [standalone]


#95932

FromLaura Creighton <lac@openend.se>
Date2015-09-03 15:02 +0200
Message-ID<mailman.65.1441285342.8327.python-list@python.org>
In reply to#95923
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

[toc] | [prev] | [next] | [standalone]


#95979

From"Sven R. Kunze" <srkunze@mail.de>
Date2015-09-04 01:01 +0200
Message-ID<mailman.103.1441321286.8327.python-list@python.org>
In reply to#95923
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web