PEP 289: Generator Expressions (please comment)

Alex Martelli aleax at aleax.it
Tue Oct 28 18:33:06 EST 2003


Rocco Moretti wrote:
   ...
> I've never bought the argument that "comma's make the tuple." All the

So how do you explain:

x = 7,

print type(x)

pray?


> To me, tuple packing/unpacking is a special case, not a general feature

That "7," up there is no packing nor unpacking.  Just a tuple.


> Of course, I'll change my opinion if Guido comes out and says that he
> consciously intended all along for tuples to be defined by commas, and

Guido doesn't follow c.l.py, so if you're interested you'll have
to ask him directly.  I think he's on record as saying so, more
than once, but can't be bothered to check -- this subthread's just
too silly and captious.


> So let me see if I have this correct:
> 
> (Long Answer)
> 
> Genex's are required to have parenthesis around them except when they
> would yield double parenthesis i.e. ((...)) - and the only place where
> this is relevant is in function calls with one argument, or with
> creating a tuple. (As genex's as base class lists and as a parameter
> list would be illegal.) But with creating a tuple, you have to have a
> comma to create a singleton, so that case doesn't matter either.

Right -- the "creating a tuple" wouldn't cause any double parentheses.


> As a parenthetical (sic.) note, the only other cases of 'double
> brackets' are {(...)} and [(...)], as python doesn't use <...> as
> brackets. The first is illegal anyway - you can't create a dictionary
> with just a genex. The second is covered in the PEP - that is:
> 
> """
> [x for x in S]    # This is a list comprehension.
> [(x for x in S)]  # This is a list containing one generator
>                    # expression.
> """
> *and* the former is depreciated in favor of list(<genex>).

I think the spelling is "deprecated" -- and while I'd love it
to be, I don't think it is.


> (Current Final short answer)
> 
> Parenthesis are required around genex's except when they are the *only*
> parameter to a function. If either the first or last expression in a
> genex is a tuple, they must have parenthesis around them.

Right, AFAIK.


Alex





More information about the Python-list mailing list