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

  • Home
  • SEARCH
  • 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 8098449
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:04:58+00:00 2026-06-05T22:04:58+00:00

I was playing around with clever ways to create a python generator for sequence

  • 0

I was playing around with clever ways to create a python generator for sequence A003602

This appears to work, but I can’t figure out why. It seems to me like it should hit infinite recursion. Is python doing some lazy evaluation somewhere that I don’t recognize?

def N():
    i=1
    while True:
        yield i
        i+=1

def interleave(a,b):
    def result():
        x=a()
        y=b()
        while True:
            yield next(x)
            yield next(y)
    return result

def RZ():
    return interleave(N,RZ)()

gen=RZ()

To me it seems like since RZ instantly calls the method returned by interleave which in turn calls b which is RZ (before the first call to yield), this should be infinite recursion. But this actually seems to work. Can anyone explain why?

  • 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-05T22:04:59+00:00Added an answer on June 5, 2026 at 10:04 pm

    Generators (any function with a yield statement) are lazy. This means that result() will not start processing until you request the first value from it, which you don’t do.

    The root cause here is that you ask for a value from x first. This means that the generator never gets to asking it’s child generator until at least the second value asked for. Consider the simpler example:

    def test():
        yield 1
        a = test()
        while True:
            yield next(a)
    
    a = test()
    for i in range(10):
        print(next(a))
    

    This works, just as yours does. It has the potential for infinite recursion, but will only get that far if you ask for that many values. All you have to do is remove the yield 1 to get the expected behaviour. In yours, just switch N and RZ and ask for the next value – you will get the expected recursion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been playing around with this for a couple of hours and can't seem to
Pleasantries I've been playing around with this idea for a couple of days but
Playing around with Python - tkInter - Entry widget - when I use validatecommand
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>
Just playing around with some of the APIs in .NET and I can't seem
I've been playing around with Simple.Data and have run across something that I can't
I'm just playing around with some code but it seemed to have the specific
I'm playing around with networkx (graph library in python) and I found documentation saying
Playing around with jQuery a bit (sorry, complete noob) I was wondering why this
Im playing around with the Devise invitable gem: https://github.com/scambra/devise_invitable It's working fine. But how

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.