[Tutor] reason(s) for trailing comma in dict declarations
eryksun
eryksun at gmail.com
Sat Aug 25 18:09:58 CEST 2012
On Sat, Aug 25, 2012 at 11:53 AM, <akleider at sonic.net> wrote:
>
> Put each entry on its own line, indented by two spaces, and leave a
> trailing comma on the last entry. The latter is especially important
> in sequences of strings to prevent them from being
> "silently"<=>"concatenated" if you were to add an entry and forget the
> comma.
> """
>
> When I first saw this I thought it would lead to a syntax error so tried
> it out..
> Then played with it to try to recreate the '"silently"<=>"concatenated"'
> problem but couldn't. I like this syntax because it avoids the syntax
> error if the comma is omitted when adding an entry but I don't understand
> the (potential) concatenation problem.
>
> Could you explain please?
Sure, I said the problem is with "sequences", not mappings. The syntax
for a dictionary will catch the mistake. But try it with a list.
x = [
"string1",
"string2",
"string3" # no comma
]
Later you decide to add "string4" but forget to add the comma:
x = [
"string1",
"string2",
"string3" # no comma
"string4"
]
Result:
['string1', 'string2', 'string3string4']
More information about the Tutor
mailing list