# iterator expression - please explain

Discussion in 'Python' started by Helmut Jarausch, May 24, 2004.

1. ### Helmut JarauschGuest

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

Helmut Jarausch, May 24, 2004

2. ### Erik Max FrancisGuest

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.alcyone.com/max/
/ \ San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
\__/ War is the province of chance.
-- Karl von Clausewitz

Erik Max Francis, May 24, 2004

3. ### Helmut JarauschGuest

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

Helmut Jarausch, May 24, 2004
4. ### Erik Max FrancisGuest

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

Erik Max Francis, May 24, 2004
5. ### YermatGuest

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

Yermat, May 24, 2004
6. ### Erik Max FrancisGuest

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

Erik Max Francis, May 24, 2004