Velocity Reviews > iterator expression - please explain

# 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

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

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

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

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

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

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

 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 OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Adem C++ 42 11-04-2008 12:39 PM Adem C Programming 45 11-04-2008 12:39 PM Lilith C++ 2 05-23-2007 05:39 AM Hendrik Maryns Java 18 12-22-2005 05:14 AM Jason luo C Programming 3 08-19-2004 12:43 AM