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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:46:10+00:00 2026-06-13T22:46:10+00:00

A generator function can be defined by putting the yield keyword in the function’s

  • 0

A generator function can be defined by putting the yield keyword in the function’s body:

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

How to define an empty generator function?

The following code doesn’t work, since Python cannot know that it is supposed to be a generator function instead of a normal function:

def empty():
    pass

I could do something like this:

def empty():
    if False:
        yield

But that would be very ugly. Is there a nicer way?

  • 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-13T22:46:11+00:00Added an answer on June 13, 2026 at 10:46 pm

    You can use return once in a generator; it stops iteration without yielding anything, and thus provides an explicit alternative to letting the function run out of scope. So use yield to turn the function into a generator, but precede it with return to terminate the generator before yielding anything.

    >>> def f():
    ...     return
    ...     yield
    ... 
    >>> list(f())
    []
    

    I’m not sure it’s that much better than what you have — it just replaces a no-op if statement with a no-op yield statement. But it is more idiomatic. Note that just using yield doesn’t work.

    >>> def f():
    ...     yield
    ... 
    >>> list(f())
    [None]
    

    Why not just use iter(())?

    This question asks specifically about an empty generator function. For that reason, I take it to be a question about the internal consistency of Python’s syntax, rather than a question about the best way to create an empty iterator in general.

    If question is actually about the best way to create an empty iterator, then you might agree with Zectbumo about using iter(()) instead. However, it’s important to observe that iter(()) doesn’t return a function! It directly returns an empty iterable. Suppose you’re working with an API that expects a callable that returns an iterable each time it’s called, just like an ordinary generator function. You’ll have to do something like this:

    def empty():
        return iter(())
    

    (Credit should go to Unutbu for giving the first correct version of this answer.)

    Now, you may find the above clearer, but I can imagine situations in which it would be less clear. Consider this example of a long list of (contrived) generator function definitions:

    def zeros():
        while True:
            yield 0
    
    def ones():
        while True:
            yield 1
    
    ...
    

    At the end of that long list, I’d rather see something with a yield in it, like this:

    def empty():
        return
        yield
    

    or, in Python 3.3 and above (as suggested by DSM), this:

    def empty():
        yield from ()
    

    The presence of the yield keyword makes it clear at the briefest glance that this is just another generator function, exactly like all the others. It takes a bit more time to see that the iter(()) version is doing the same thing.

    It’s a subtle difference, but I honestly think the yield-based functions are more readable and maintainable.

    See also this great answer from user3840170 that uses dis to show another reason why this approach is preferable: it emits the fewest instructions when compiled.

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

Sidebar

Related Questions

Let's consider the following scenario: a function which can generate code colors from white
If function accepts structural type, it can be defined as: def doTheThings(duck: { def
I have a function to generate an 11 character salt. This salt can include
Consider a tuple v = (a,b,c) and a generator function generate(x) which receives an
I have three lists of lists, and I'm trying to write a generator function
I have this function in my generator. Private Sub AddBoundedValue(ByVal boundedValue As Object, ByVal
Suppose I have a function that returns a closure: sub generator { my $resource
In my model there are an entity generator, some attribute function(out_attrName) and an entity
I'm trying to run the code from here MDN: Generators and Iterators function fib()
From the MSDN docs for create function : User-defined functions cannot be used to

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.