Generator expression parenthesis mixed with function call ones

  • Thread starter Laurent Pointal
  • Start date
L

Laurent Pointal

[Python 2.4.3 (#69, Mar 29 2006, 17:35:34) [MSC v.1310 32 bit (Intel)]
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.
 
F

Facundo Batista

Laurent Pointal wrote:

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:
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument


The correct way to do that is:
4 (<generator object at 0xb7dab56c>,)

Regards,
 
L

Laurent Pointal

Facundo Batista a écrit :
Laurent Pointal wrote:



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 ?
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:

File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole argument


The correct way to do that is:

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.
 
G

Gabriel Genellina

En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal
File "<stdin>", line 1
SyntaxError: invalid syntax

2.5 has a better error message:
py> f(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.
 
L

Laurent Pointal

Dennis said:
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.
 
L

Laurent Pointal

Gabriel said:
En Wed, 07 Mar 2007 12:53:43 -0300, Laurent Pointal


2.5 has a better error message:
py> f(4,i for i in range(10))
File "<stdin>", line 1
SyntaxError: Generator expression must be parenthesized if not sole
argument


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.
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.
 
G

Gabriel Genellina

En Wed, 07 Mar 2007 15:21:57 -0300, Laurent Pointal
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.html#SECTION00111100000000000000000

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/python-dev/2003-October/038868.html
 
D

Dennis Lee Bieber

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...

A list-comp produces the entire list at "comprehension time", and
returns the list as its value.

As I understand generators (are they even in 2.4?) they only return
values on an as-needed basis -- so in effect, they are a single argument
on a function call; equivalent to a function. One wouldn't need ()
around a lambda expression (to my knowledge)...

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top