iterator expression - please explain

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

  1. 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
    #1
    1. Advertising

  2. 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
    #2
    1. Advertising

  3. 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
    #3
  4. 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
    #4
  5. Helmut Jarausch

    Yermat Guest

    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
    #5
  6. 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
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hendrik Maryns
    Replies:
    18
    Views:
    1,446
  2. KK
    Replies:
    2
    Views:
    646
    Big Brian
    Oct 14, 2003
  3. Jason luo
    Replies:
    3
    Views:
    560
    Peter Ammon
    Aug 19, 2004
  4. Lilith
    Replies:
    2
    Views:
    245
    Lilith
    May 23, 2007
  5. Kaye Ng
    Replies:
    8
    Views:
    182
    Josh Cheek
    Jun 8, 2010
Loading...

Share This Page