By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,279 Members | 2,178 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,279 IT Pros & Developers. It's quick & easy.

Generator expression parenthesis mixed with function call ones

P: n/a
[Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
on win32]

Given the following:
>>sum(i for i in range(10))
45
>>def f(*args) : print args
....
>>f(i for i in range(10))
(<generator object at 0x00A79788>,)
>>def f(a,*args) : print a,ar
....
>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: invalid syntax
Why does Python allow generator expression parenthesis to be mixed with
function call parenthesis when there is only one parameter ?

IMHO this should be forbidden, usage must not be different when there is
only one parameter and when there are more parameters.
User should all times explicitly use () for a generator expression and
[] for a list comprehension expression.
Mar 7 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Laurent Pointal wrote:

>>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: invalid syntax
Why does Python allow generator expression parenthesis to be mixed with
function call parenthesis when there is only one parameter ?
For simplicity and elegant coding, so you can do something like you did
at first:

sum(i for i in range(10))

IMHO this should be forbidden, usage must not be different when there is
only one parameter and when there are more parameters.
The problem in your last test is that if you use more than one argument,
you *must* use the parenthesis. In Py2.5 there's a better message error:
>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument
The correct way to do that is:
>>f(4,(i for i in range(10)))
4 (<generator object at 0xb7dab56c>,)

Regards,

--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
Mar 7 '07 #2

P: n/a
Facundo Batista a écrit :
Laurent Pointal wrote:

>>>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: invalid syntax
Why does Python allow generator expression parenthesis to be mixed with
function call parenthesis when there is only one parameter ?

For simplicity and elegant coding, so you can do something like you did
at first:

sum(i for i in range(10))
How a Python beginner know that he is using a generator and not a
list-comprehension ?
>IMHO this should be forbidden, usage must not be different when there is
only one parameter and when there are more parameters.

The problem in your last test is that if you use more than one argument,
you *must* use the parenthesis. In Py2.5 there's a better message error:
>>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument
The correct way to do that is:
>>>f(4,(i for i in range(10)))
4 (<generator object at 0xb7dab56c>,)
Thanks, I know. My example is just to show a point I consider to be
non-coherent (different processing if there is one argument or more than
one).

A+

Laurent.

Mar 7 '07 #3

P: n/a
En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
<la*************@limsi.frescribió:
>>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: invalid syntax
2.5 has a better error message:
pyf(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole
argument
Why does Python allow generator expression parenthesis to be mixed with
function call parenthesis when there is only one parameter ?
Because they are redundant when only one argument is used.
sum(i for i in range(10)) looks better than sum((i for i in range(10)))
"Beautiful is better than ugly", and "Readability counts."
IMHO this should be forbidden, usage must not be different when there is
only one parameter and when there are more parameters.
It's similar to "%d" % 123 vs. "%d" % (123,)
"""Special cases aren't special enough to break the rules.
Although practicality beats purity."""
User should all times explicitly use () for a generator expression and
[] for a list comprehension expression.
For a list comprehension, yes. For a generator, not always.

--
Gabriel Genellina

Mar 7 '07 #4

P: n/a
Dennis Lee Bieber wrote:
On Wed, 07 Mar 2007 17:15:33 +0100, Laurent Pointal
<la*************@limsi.frdeclaimed the following in comp.lang.python:
>>
How a Python beginner know that he is using a generator and not a
list-comprehension ?
A list comprehension ALWAYS has list brackets [...] around it...
Yes, and a generator expression ALWAYS has round brackets... which can be
confused with function call ones when it is used in a single argument
function call...
I still personnaly think function call round brackets and generator
expression round brackets should both be present.

Mar 7 '07 #5

P: n/a
Gabriel Genellina wrote:
En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
<la*************@limsi.frescribió:
>>>>f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: invalid syntax

2.5 has a better error message:
pyf(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole
argument
>Why does Python allow generator expression parenthesis to be mixed with
function call parenthesis when there is only one parameter ?

Because they are redundant when only one argument is used.
sum(i for i in range(10)) looks better than sum((i for i in range(10)))
"Beautiful is better than ugly", and "Readability counts."
I complement my reply.

Beginners generally know about list-comprehensions and associate the
syntax "x for x in asequence" to a list expression. I'm not sure that
reading a "f(i for i in range(20))" they understand that they are dealing
with a different object kind.

If function f start by a if len(myparameter)...
TypeError: len() of unsized object

If function f goes among its parameter with "for x in myparameter" more than
once, other loops goes throught an empty loop.
>IMHO this should be forbidden, usage must not be different when there is
only one parameter and when there are more parameters.

It's similar to "%d" % 123 vs. "%d" % (123,)
"""Special cases aren't special enough to break the rules.
Although practicality beats purity."""
In that case there cannot be confusion.

A+

Laurent.

Mar 7 '07 #6

P: n/a
En Wed, 07 Mar 2007 15:21:57 -0300, Laurent Pointal
<la*************@wanadoo.frescribió:
Gabriel Genellina wrote:
>En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
<la*************@limsi.frescribió:
>>Why does Python allow generator expression parenthesis to be mixed with
function call parenthesis when there is only one parameter ?
Beginners generally know about list-comprehensions and associate the
syntax "x for x in asequence" to a list expression. I'm not sure that
But list comprehensions have [] around... When you don't see the [], you
should think "this is something different"...
reading a "f(i for i in range(20))" they understand that they are dealing
with a different object kind.
Exactly.
If function f start by a if len(myparameter)...
TypeError: len() of unsized object

If function f goes among its parameter with "for x in myparameter" more
than
once, other loops goes throught an empty loop.
Then he thinks "Something is wrong!", and reads some Python books, or asks
here, and somebody will point him to the Python Tutorial, section 9.11:
http://docs.python.org/tut/node11.ht...00000000000000
>>IMHO this should be forbidden, usage must not be different when there
is only one parameter and when there are more parameters.
If you want to review the original discussion on how to spell a generator
expression (called "accumulator display" by that time) see
http://mail.python.org/pipermail/pyt...er/038868.html

--
Gabriel Genellina

Mar 7 '07 #7

P: n/a
Gabriel Genellina a écrit :
If you want to review the original discussion on how to spell a
generator expression (called "accumulator display" by that time) see
http://mail.python.org/pipermail/pyt...er/038868.html
That's a long discussion... and it seem I'm not alone to dislike parens
confusion.

http://mail.python.org/pipermail/pyt...er/038906.html

Thanks for the pointer.

Laurent.
Mar 8 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.