[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. 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/
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.
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
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.
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
...
|
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...
|
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)
...
|
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...
|
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(){
|
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():
|
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...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |