Skip to content

Faq 4.28 -- Trailing comas #46672

Closed
Closed

Description

@dubiel
mannequin
BPO 2420
Nosy @akuchling, @birkenfeld

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/akuchling'
closed_at = <Date 2008-09-07.00:00:04.479>
created_at = <Date 2008-03-19.08:50:46.819>
labels = ['docs']
title = 'Faq 4.28 -- Trailing comas'
updated_at = <Date 2008-09-07.00:00:04.423>
user = 'https://bugs.python.org/dubiel'

bugs.python.org fields:

activity = <Date 2008-09-07.00:00:04.423>
actor = 'akuchling'
assignee = 'akuchling'
closed = True
closed_date = <Date 2008-09-07.00:00:04.479>
closer = 'akuchling'
components = ['Documentation']
creation = <Date 2008-03-19.08:50:46.819>
creator = 'dubiel'
dependencies = []
files = []
hgrepos = []
issue_num = 2420
keywords = []
message_count = 3.0
messages = ['64050', '64263', '72722']
nosy_count = 3.0
nosy_names = ['akuchling', 'georg.brandl', 'dubiel']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue2420'
versions = []

Activity

  1. dubiel commented on Mar 19, 2008

    dubielmannequin
    MannequinAuthor

    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.

    1. 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.

    1. 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.

    2. 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.

    3. 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

  2. birkenfeld commented on Mar 21, 2008

    @birkenfeld
    Member

    I think AMK maintains the FAQLs.

  3. akuchling commented on Sep 7, 2008

    @akuchling
    Contributor

    I've edited the suggested text and added it to the FAQ
    in rev. 11722. Thanks for your contribution!

  4. transferred this issue fromon Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

docsDocumentation in the Doc dir

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions