Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > iterator expression - please explain

Reply
Thread Tools

iterator expression - please explain

 
 
Helmut Jarausch
Guest
Posts: n/a
 
      05-24-2004
Hi,

with Python 2.4a0 (CVS 2004/05/24)
I get

def myfun(myarg):
for z in myarg:
print z

myfun(x^2 for x in xrange(10))
2
3
0
1
6
7
4
5
10
11

I expected the sequence 0,1,4,9,....
What am I missing and what's going on here?

Many thanks for a hint,
Helmut.

---
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
Reply With Quote
 
 
 
 
Erik Max Francis
Guest
Posts: n/a
 
      05-24-2004
Helmut Jarausch wrote:

> myfun(x^2 for x in xrange(10))

...
> I expected the sequence 0,1,4,9,....
> What am I missing and what's going on here?


^ is the bitwise xor operator. You wanted **. (Although what you wrote
above isn't legal anyway.)

--
__ Erik Max Francis && http://www.velocityreviews.com/forums/(E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ War is the province of chance.
-- Karl von Clausewitz
 
Reply With Quote
 
 
 
 
Helmut Jarausch
Guest
Posts: n/a
 
      05-24-2004
Erik Max Francis wrote:
> Helmut Jarausch wrote:
>
>
>>myfun(x^2 for x in xrange(10))

>
> ...
>
>>I expected the sequence 0,1,4,9,....
>>What am I missing and what's going on here?

>
>
> ^ is the bitwise xor operator. You wanted **. (Although what you wrote
> above isn't legal anyway.)
>

Many thanks for pointing out that silly mistake.
But why isn't it legal?
By the way, the corrected version (with **) works just fine (with the
CVS version of Python from the weekend or today)

Helmut.

--
Helmut Jarausch

Lehrstuhl fuer Numerische Mathematik
RWTH - Aachen University
D 52056 Aachen, Germany
 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      05-24-2004
Helmut Jarausch wrote:

> Many thanks for pointing out that silly mistake.
> But why isn't it legal?


It's not legal because you typed it in, rather than cutting and pasting
it. You meant:

myfun([x**2 for x in xrange(10)])

--
__ Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Chastity the most unnatural of the sexual perversions.
-- Aldous Huxley
 
Reply With Quote
 
Yermat
Guest
Posts: n/a
 
      05-24-2004
Erik Max Francis wrote:
> Helmut Jarausch wrote:
>
>
>>Many thanks for pointing out that silly mistake.
>>But why isn't it legal?

>
>
> It's not legal because you typed it in, rather than cutting and pasting
> it. You meant:
>
> myfun([x**2 for x in xrange(10)])
>


I don't think so ! It seems to be the new feature "generator expression"
or something like that...

See http://www.python.org/peps/pep-0289.html

--
Yermat

 
Reply With Quote
 
Erik Max Francis
Guest
Posts: n/a
 
      05-24-2004
Yermat wrote:

> I don't think so ! It seems to be the new feature "generator
> expression"
> or something like that...


Oh, sorry about that, you said you were using the latest version from
CVS but it didn't register.

--
__ Erik Max Francis && (E-Mail Removed) && http://www.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ Chastity the most unnatural of the sexual perversions.
-- Aldous Huxley
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C++ 42 11-04-2008 12:39 PM
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" Adem C Programming 45 11-04-2008 12:39 PM
Please explain what this expression means Lilith C++ 2 05-23-2007 05:39 AM
Difference between Java iterator and iterator in Gang of Four Hendrik Maryns Java 18 12-22-2005 05:14 AM
How to explain "evaluate the expression as a void expression"? Jason luo C Programming 3 08-19-2004 12:43 AM



Advertisments