Trailing comma (was: Re: structure in Python)

Skip Montanaro skip at pobox.com
Tue Oct 21 17:38:42 CEST 2003


 
    Cousin>   I don't understand how an extra comma at the end of a sequence
    Cousin>   is supposed to help with problems like this where a sequence
    Cousin>   delimeter, the comma, has been ommitted in the middle of the
    Cousin>   sequence ....

Today, I write:

    weekdays = [
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday"
               ]

An astute reader points out that I forgot Sunday, so I add a line:

    weekdays = [
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday"
                "Sunday"
               ]

The problem can occur in other ways as well.  Suppose you've painfully
constructed a long list (dozens? hundreds?) of string constants (and you're
not smart enough to realize you should initialize the list from a data file)
then decide it would be easier to maintain that growing list if you kept it
sorted.

    weekdays = [
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday"
               ]

becomes

    weekdays = [
                "Friday",
                "Saturday",
                "Sunday"
                "Thursday",
                "Wednesday",
                "Monday",
                "Tuesday",
               ]

after selecting the rows containing weekdays in Emacs and typing

    C-u ESC | sort RET

Allowing and using a trailing comma prevents those sort of subtle mistakes.

    Cousin>   I am, of course, grateful to the original coder for saving me
    Cousin>   the 42e-19 ergs of energy required to press the comma key in
    Cousin>   cases where I would further extend the sequence at its end,
    Cousin>   but personally find this style convention U G L Y and a
    Cousin>   possible source of confusion ....

Practicality beats purity.  If it's a constant list which you are certain
you will never extend or reorder, feel free to omit that last comma.  Also,
if the list is not a list of string constants there's no real harm in
omitting it either, because if you extend or reorder the list and forget to
insert a comma in the right place you'll get a SyntaxError the next time you
import that module.  The only case where it's problematic is for lists of
string constants where "abc" "def" is valid syntax.  In this case the Python
bytecode compiler can't help you.

    Cousin>   This probably stems from the fact that my elementary school
    Cousin>   grammar teacher would whack me severely about the head and
    Cousin>   shoulders if I wrote ....

    Cousin>      Uncle Scrooge took Huey, Duey, and Louie, 
    Cousin>      to the park. 

Perhaps, but your Python teacher would have praised you. ;-)

Skip





More information about the Python-list mailing list