iterator expression - please explain

H

Helmut Jarausch

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
 
E

Erik Max Francis

Helmut said:
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.)
 
H

Helmut Jarausch

Erik said:
Helmut Jarausch wrote:




^ 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
 
E

Erik Max Francis

Helmut said:
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)])
 
E

Erik Max Francis

Yermat said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top