473,396 Members | 2,109 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generator expression parenthesis mixed with function call ones

[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
7 8092
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: snacktime | last post by:
I need to convert a generator expression to a list expression so it will work under python 2.3. I rewrote this: for c in range(128): even_odd = (sum(bool(c & 1<<b) for b in range(8))) & 1 ...
16
by: Peter Otten | last post by:
To confuse a newbies and old hands alike, Bengt Richter wrote: > Need something more straightforward, e.g., a wrapped one-liner: > > >>> def guess(n=3): print ("You're right!", 'No more tries...
9
by: bonono | last post by:
Hi, I initially thought that generator/generator expression is cool(sort of like the lazy evaluation in Haskell) until I notice this side effect. >>>a=(x for x in range(2)) >>>list(a) ...
7
by: Deniz Bahar | last post by:
The top level of C's precedence table has () next to , I was wondering what the () refers to. Some tables say in a footnote that () is for function calls not parenthesis, other tables say the...
8
by: optimistx | last post by:
In excellent YAHOO user interface programs I see often extra parenthesis like this (foo=function(){ alert('I am foo'); })(); instead of bar=function(){
6
by: Paddy | last post by:
Hi, # If I have a function definition def f1(arg): global capturecall if capturecall: ... do_normal_stuff(arg) # and its later use: def f2():
0
by: Maric Michaud | last post by:
I faced a strange behavior with generator expression, which seems like a bug, for both python 2.4 and 2.5 : .... a = 1, 2, 3 .... b = 1, 2, 3 .... C = list((e,f) for e in a for f...
1
by: nlulla | last post by:
Hi I am trying to get all rows from a datatable where the first column F1 is a date, as this datatable is created of excel, i only want to deal with rows where the first column is having a date in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.