[Python-talk] Reflections on tuples, September 2005, 2 of 2

Kent Johnson kent37 at tds.net
Fri Sep 23 23:30:42 EDT 2005


> On Fri, 2005-09-23 at 18:00 -0400, Bill Sconce wrote:
>>(**) and that parentheses are used ONLY to group things (**).
>>
>>I _think_ we can get by with this, if "group things" can be
>>taken to include grouping things for precedence of operations,
>>specifying formal parameters for definitions of functions & 
>>classes, etc., and passing parameters to an invocation.  

Parentheses are required as part of a generator comprehension. The parentheses can have other syntactic significance, e.g. the parens in a function call, but they are required.

Without parens it's a syntax error:
 >>> x for x in range(10)
  File "<stdin>", line 1
    x for x in range(10)
        ^
SyntaxError: invalid syntax

With parens it's a valid expression whose value is a generator object
 >>> (x for x in range(10))
<generator object at 0x008E4490>

The same parens can be used for a function call:
 >>> list(x for x in range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

A little strange but it works...

Kent



More information about the Python-talk mailing list