Strange location for a comma

MRAB python at mrabarnett.plus.com
Thu Sep 3 08:50:41 EDT 2015


On 2015-09-03 13:28, ast wrote:
>
> "ast" <nomail at 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).



More information about the Python-list mailing list