classification
Title: Faq 4.28 -- Trailing comas
Type: Stage:
Components: Documentation Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: akuchling Nosy List: akuchling, dubiel, georg.brandl
Priority: normal Keywords:

Created on 2008-03-19 08:50 by dubiel, last changed 2008-09-07 00:00 by akuchling. This issue is now closed.

Messages (3)
msg64050 - (view) Author: Leszek Dubiel (dubiel) Date: 2008-03-19 08:50
This is after discussion on python-ideas: 

http://mail.python.org/pipermail/python-ideas/2008-March/001488.html



I would suggest to add question 4.28 to faq. Everybody who learns Python
reads that and will not ask questions about "one-element tuples". I have
compiled answer from responses to my last question about one-element tuple.



Question: Why python allows to put comma at the end of list? This looks
ugly and seems to break common rules...

Answer: There are may reasons that follow.

1. If you defined multiline dictionary

       d = {                  
           "A": [1, 5],
           "B": [6, 7],  # last trailing comma is optional but good style
       }

it would be easier to add more elements, because you don't have to care
about colons -- you always put colon at the end of line and don't have
to reedit other lines. It eases sorting of such lines too -- just cut
line and paste above.

2. Missing comma can lead to errors that are hard to diagnose. For example:

       x = [
         "fee",
         "fie"
         "foo",
         "fum"
       ]

contains tree elements "fee", "fiefoo" and "fum". So if programmer puts
comma always at the end of line he saves lots of trouble in a future.


2. Nearly all reasonable programming languages (C, C++, Java) allow for an
extra trailing comma in a comma-delimited list, for consistency and to
make programmatic code generation easier. So for example [1,2,3,] is
intentionally correct.


3. Creating one-element tuples using tuple(['hello']) syntax is much much
slower (a factor of 20x here) then writing just ('hello', ).  Trailing
commas
when you only have one item are how python tuple syntax is defined
allowing them to use commas instead of needing other tokens. If python
didn't allow comma at the end of tuple, you will have to use such slow
syntax.


4. The same rule applies to other type of lists, where delimiter can occur
at the end. For example both strings "alfa\nbeta\n" and "alfa\nbeta" contain
two lines.



Sources:

-- http://mail.python.org/pipermail/python-list/2003-October/231419.html
-- http://mail.python.org/pipermail/python-ideas/2008-March/001478.html
-- http://mail.python.org/pipermail/python-ideas/2008-March/001475.html
msg64263 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-03-21 19:43
I think AMK maintains the FAQLs.
msg72722 - (view) Author: A.M. Kuchling (akuchling) * (Python committer) Date: 2008-09-07 00:00
I've edited the suggested text and added it to the FAQ
in rev. 11722.  Thanks for your contribution!
History
Date User Action Args
2008-09-07 00:00:04akuchlingsetstatus: open -> closed
resolution: fixed
messages: + msg72722
2008-03-21 19:43:08georg.brandlsetassignee: georg.brandl -> akuchling
messages: + msg64263
nosy: + akuchling
2008-03-19 08:50:46dubielcreate