Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7912771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:41:27+00:00 2026-06-03T13:41:27+00:00

Say I’ve got a generator: def mygen(): for i in range(10): yield i This

  • 0

Say I’ve got a generator:

def mygen():
    for i in range(10):
        yield i

This works as I would expect: all combinations of i and j

for i in mygen():
    for j in mygen():
        print i, j

I would think these are different instances. Why are they not acting as different instances?

g1 = mygen()
g2 = mygen()

for i in g1:
    for j in g2:
        print i, j

If I try g1.next(), I get an error because there is no data left.

I’m running Python 2.7.1.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:41:28+00:00Added an answer on June 3, 2026 at 1:41 pm

    Iterating over g2 the first time consumes it, so there’s nothing left when you try to iterate over it subsequent times.

    g1 = mygen()
    for i in g1:
        g2 = mygen()
        for j in g2:
            print i, j
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I got this C++ code: class class1{ class2 *x; } class class2{ class1
Let's say I'm outputting a post title and in our database, it's Hello Y’all
Say I have this: private list<myClass> myCollection; Is there a programming idiom to shorten
Say I have this: for( i = 0; i < 10; i++ ) {
Say I have this anonymous function: (function(window){ var private = 'private msg'; function sayit()
Say I have a collection with objects like this, where the ts property contains
Say I wanted to run 'User.all.each{|u| u.destroy}' from a shell script called killallusers.sh. How
Say I have this variables below ( id , a,b,c,d ) id a b
Say you have a 4-node J2EE application server cluster, all running instances of a
Say I have two classes like this: class A{ private static Random random =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.