copying generatrors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Horace  Enea

    copying generatrors

    Does anyone have code to copy a generator?

    Here is what I'd like to do:

    def foo():
    yield 1
    yield 2
    yield 3

    f = foo()
    g = copy(foo)

    print f.next()
    1
    print f.next()
    2

    print g.next()
    1

    Thanks,

    Horace
  • Matimus

    #2
    Re: copying generatrors

    Why not just do this:
    >>def foo():
    .... yield 1
    .... yield 2
    .... yield 3
    ....
    >>f = foo()
    >>g = foo()
    >>f.next()
    1
    >>f.next()
    2
    >>f.next()
    3
    >>g.next()
    1
    >>g.next()
    2
    >>g.next()
    3

    Comment

    • Horace  Enea

      #3
      Re: copying generatrors

      My example wasn't very good. Here's another try:

      def foo():
      yield 1
      yield 2
      yield 3

      f = foo()
      f.next()
      1

      g=copy(f) # copy the generator after an iteration

      f.next()
      2
      f.next()
      3

      g.next()
      2

      I want to copy the generator's state after one or more iterations.





      In article <1181087423.361 999.324710@o11g 2000prd.googleg roups.com>,
      Matimus <mccredie@gmail .comwrote:
      Why not just do this:
      >
      >def foo():
      ... yield 1
      ... yield 2
      ... yield 3
      ...
      >f = foo()
      >g = foo()
      >f.next()
      1
      >f.next()
      2
      >f.next()
      3
      >g.next()
      1
      >g.next()
      2
      >g.next()
      3

      Comment

      • Steven Bethard

        #4
        Re: copying generatrors

        Horace Enea wrote:
        My example wasn't very good. Here's another try:
        >
        def foo():
        yield 1
        yield 2
        yield 3
        >
        f = foo()
        f.next()
        1
        >
        g=copy(f) # copy the generator after an iteration
        >
        f.next()
        2
        f.next()
        3
        >
        g.next()
        2
        >
        I want to copy the generator's state after one or more iterations.
        You could use itertools.tee() :
        >>def foo():
        .... yield 1
        .... yield 2
        .... yield 3
        ....
        >>import itertools
        >>f = foo()
        >>f.next()
        1
        >>f, g = itertools.tee(f )
        >>f.next()
        2
        >>f.next()
        3
        >>g.next()
        2
        >>g.next()
        3

        But note that if your iterators get really out of sync, you could have a
        lot of elements stored in memory.

        STeVe

        Comment

        • Horace  Enea

          #5
          Re: copying generatrors

          Steve,

          Hey, thanks. I'll try that.

          Horace

          In article <-JKdnb0ntvytmvvb nZ2dnUVZ_o_inZ2 d@comcast.com>,
          Steven Bethard <steven.bethard @gmail.comwrote :
          Horace Enea wrote:
          My example wasn't very good. Here's another try:

          def foo():
          yield 1
          yield 2
          yield 3

          f = foo()
          f.next()
          1

          g=copy(f) # copy the generator after an iteration

          f.next()
          2
          f.next()
          3

          g.next()
          2

          I want to copy the generator's state after one or more iterations.
          >
          You could use itertools.tee() :
          >
          >>def foo():
          ... yield 1
          ... yield 2
          ... yield 3
          ...
          >>import itertools
          >>f = foo()
          >>f.next()
          1
          >>f, g = itertools.tee(f )
          >>f.next()
          2
          >>f.next()
          3
          >>g.next()
          2
          >>g.next()
          3
          >
          But note that if your iterators get really out of sync, you could have a
          lot of elements stored in memory.
          >
          STeVe

          Comment

          Working...