"1,+2,", but not "(1,)+2,"

Jon Ribbens jon+usenet at unequivocal.eu
Fri Jul 31 14:25:00 EDT 2020


On 2020-07-31, Bart <bc at freeuk.com> wrote:
> Not sure about the trailing commas on each. It seems Python ignores 
> trailing commas on tuple constructors, so that the A,B, would be a 
> 2-tuple, and A+B, would have been a 1-tuple if A+B had been legal.

It's not just tuples, it's lists, sets, dictionaries, etc. It's very
useful because it means that when editing source code to add new items
or to rearrange items you don't need to worry about the commas. e.g.:

    animals = {
        'cow': 'mammal',
        'spider': 'arachnid',
        'turtle': 'reptile',
    }

If you want to add a new item to or remove an existing item from the
dictionary, you only need to edit one line, and it doesn't matter if
that line is in the middle or at the end of the existing entries.


More information about the Python-list mailing list