Hard to find bugs, misleading exceptions

Steven Taschuk staschuk at telusplanet.net
Tue Feb 25 12:42:40 EST 2003


Quoth Chermside, Michael:
  [...]
> (2) Also, type it like this:
> 
>     el = [
>         (1,2),
>         (3,4),
>         (5,6),
>         (7,8),   # note USE of comma
>     ]
> 
>   The use of a comma after EVERY element, instead of
>   every one but the last, will save you lots of bugs
>   like this when cutting-and-pasting or adding new
>   items to the list. Fortunately, it's LEGAL syntax
>   in Python (although not, unfortunately, in C, Java,
>   and some other things I have to work with).

Que?

    $ cat foo.java
    public class foo {
            public static void main(String[] args) {
                    int a[] = { 1, 2, 3, }; // trailing comma
                    for (int i = 0; i < a.length; i++) {
                            System.out.println(a[i]);
                    }
            }
    }
    $ javac foo.java
    $ java foo
    1
    2
    3
    $ cat foo.c
    #include <stdio.h>
    int main(void) {
            int i, a[] = { 1, 2, 3, }; /* trailing comma */
            for (i = 0; i < 3; i++) {
                    printf("%d\n", a[i]);
            }
    }
    $ gcc -o foo foo.c
    $ ./foo
    1
    2
    3
    $

I don't have a reference for ISO C, but section 10.6 of the Java
Language Specification (2nd Ed.) clearly permits the trailing
comma.

-- 
Steven Taschuk                            staschuk at telusplanet.net
Every public frenzy produces legislation purporting to address it.
                                           (Kinsley's Law)





More information about the Python-list mailing list