RE: [Python-Dev] Multi-line import implementation (was: 2.4a2, and @decorators)

My patch (attached) accepts
import (os, sys) from sys import (stdin, stdout, stderr) import (os) from sys import (*)
but rejects
from (sys) import stdin import (os), (sys) import (os,)
Should any of those be allowed? Anything that I missed?
Others have already mentioned that "from sys import xxx, xxx, xxx" is the case that badly needs line wrapping. I would, however, like to suggest that (if it's easy to do) you allow a trailing comma when parenthesees are used. Of course this looks silly: from mymodule import (xxx, xxx, xxx, xxx,) But in the most extreme case, like this: from myBigModule import ( xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, xxxxxxxx, ) it's awefully nice on cut-and-paste to allow the trailing comma. But the most important reason for doing so is that Python already allows (and disregards) an extra trailing comma in similar lists: >>> [1,2,3,] [1, 2, 3] >>> {1:1,2:2,3:3,} {1: 1, 2: 2, 3: 3} >>> def func(a,b,c,): ... print a, b, c ... >>> func(1,2,3) 1 2 3 >>> (1,2,3,) (1, 2, 3) -- Michael Chermside PS: The length-one-tuple is a special case (and a bit of a wart) in that it MANDATES the use of the trailing comma. But that's got nothing to do with the basic principle.

Others have already mentioned that "from sys import xxx, xxx, xxx" is the case that badly needs line wrapping. I would, however, like to suggest that (if it's easy to do) you allow a trailing comma when parentheses are used. [...]
+1 --Guido van Rossum (home page: http://www.python.org/~guido/)

Guido van Rossum <guido@python.org> wrote:
Michael Chermside wrote:
Others have already mentioned that "from sys import xxx, xxx, xxx" is the case that badly needs line wrapping. I would, however, like to suggest that (if it's easy to do) you allow a trailing comma when parentheses are used. [...]
+1
Certainly easy to do (in fact, changing the grammar was almost sufficient). I've uploaded a new patch to SF, with the comment saying, in part, - Support trailing comma in from list (suggested by Michael Chermside on python-dev). This form is now acceptable: from os import (path,) but this is not: from os import path, I originally wanted to support the latter for consistency, but I don't think it's necessary (who would want to do that but not use parens?) and it's potentially confusing in a case like this: from sys import stdin, stdout which looks like it was intended to be one statement, but the second line is actually separate. The way the grammar is written, the case with a trailing comma but no parentheses parses okay, but it is intentionally rejected by the compiler. If we want to support that case for some reason, it's simple enough to remove the rejection test from the compiler. Dima.
participants (3)
-
Dima Dorfman
-
Guido van Rossum
-
Michael Chermside