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 8994937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:32:06+00:00 2026-06-15T23:32:06+00:00

So, I defined a simple generator: def gen1(x): if x <= 10: yield x

  • 0

So, I defined a simple generator:

def gen1(x):
    if x <= 10:
        yield x
        for v in gen1(x + 1):
            yield v

Basically, I want to decorate it so it returns all the values, but the last:

def dec(gen):

    def new_gen(x):
        g = gen(x)
        value = g.next()
        for v in g:
            yield value
            value = v

    return new_gen

Now, if I redefine gen1

@dec
def gen1(x):
    ...

for i in gen1(1):
    print i    # Nothing printed

but if I use:

some_gen = dec(gen1)

for i in some_gen(1):
    print i    # Prints 1 to 9, as needed

Why my decorator doesn’t work and how can I fix it?

  • 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-15T23:32:08+00:00Added an answer on June 15, 2026 at 11:32 pm

    The recursive invocation of your gen1 is also subject to your decorator, so everything gets consumed by the decorator.

    The simplest fix is to write the generator in non-recursive style, or to encapsulate the recursion:

    Encapsulated:

    @dec
    def gen1(x):
        def inner(x):
            if x <= 10:
                yield x
                for v in inner(x + 1):
                    yield v
        return inner(x)
    

    Non-recursive:

    @dec
    def gen1(x):
        for v in range(x, 11):
            yield v
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I defined a simple Document, like this: class Company(Document): screen_name = StringField(max_length=100, required=True, unique=True)
I've got a simple class defined as: public class MyClass { //Some properties public
I have a pretty simple UIScrollView defined inside Interface Builder. I've pasted in some
I have a very simple dialog defined as: import android.app.AlertDialog; import android.content.Context; import android.view.LayoutInflater;
I'm using WPF with .NET 3.0. I have a relatively simple DataTemplate defined as
I have a VERY simple 3D space defined in WPF, which defines a 3D
I've defined the following route for a simple blog. routes.MapRoute( Blog, blog/{year}/{month}/{day}, new {
I have a simple temp-table defined in SQL Server 2008 R2 representing a parent-child
I've got a simple Django-TastyPie API defined (it is a basic class that derives
Is there a simple preprocessor macro that is defined for a 64-bit build? I

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.