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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:34:32+00:00 2026-05-13T10:34:32+00:00

In Python, is there any difference between creating a generator object through a generator

  • 0

In Python, is there any difference between creating a generator object through a generator expression versus using the yield statement?

Using yield:

def Generator(x, y):
    for i in xrange(x):
        for j in xrange(y):
            yield(i, j)

Using generator expression:

def Generator(x, y):
    return ((i, j) for i in xrange(x) for j in xrange(y))

Both functions return generator objects, which produce tuples, e.g. (0,0), (0,1) etc.

Any advantages of one or the other? Thoughts?

  • 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-05-13T10:34:32+00:00Added an answer on May 13, 2026 at 10:34 am

    There are only slight differences in the two. You can use the dis module to examine this sort of thing for yourself.

    Edit: My first version decompiled the generator expression created at module-scope in the interactive prompt. That’s slightly different from the OP’s version with it used inside a function. I’ve modified this to match the actual case in the question.

    As you can see below, the “yield” generator (first case) has three extra instructions in the setup, but from the first FOR_ITER they differ in only one respect: the “yield” approach uses a LOAD_FAST in place of a LOAD_DEREF inside the loop. The LOAD_DEREF is “rather slower” than LOAD_FAST, so it makes the “yield” version slightly faster than the generator expression for large enough values of x (the outer loop) because the value of y is loaded slightly faster on each pass. For smaller values of x it would be slightly slower because of the extra overhead of the setup code.

    It might also be worth pointing out that the generator expression would usually be used inline in the code, rather than wrapping it with the function like that. That would remove a bit of the setup overhead and keep the generator expression slightly faster for smaller loop values even if LOAD_FAST gave the “yield” version an advantage otherwise.

    In neither case would the performance difference be enough to justify deciding between one or the other. Readability counts far more, so use whichever feels most readable for the situation at hand.

    >>> def Generator(x, y):
    ...     for i in xrange(x):
    ...         for j in xrange(y):
    ...             yield(i, j)
    ...
    >>> dis.dis(Generator)
      2           0 SETUP_LOOP              54 (to 57)
                  3 LOAD_GLOBAL              0 (xrange)
                  6 LOAD_FAST                0 (x)
                  9 CALL_FUNCTION            1
                 12 GET_ITER
            >>   13 FOR_ITER                40 (to 56)
                 16 STORE_FAST               2 (i)
    
      3          19 SETUP_LOOP              31 (to 53)
                 22 LOAD_GLOBAL              0 (xrange)
                 25 LOAD_FAST                1 (y)
                 28 CALL_FUNCTION            1
                 31 GET_ITER
            >>   32 FOR_ITER                17 (to 52)
                 35 STORE_FAST               3 (j)
    
      4          38 LOAD_FAST                2 (i)
                 41 LOAD_FAST                3 (j)
                 44 BUILD_TUPLE              2
                 47 YIELD_VALUE
                 48 POP_TOP
                 49 JUMP_ABSOLUTE           32
            >>   52 POP_BLOCK
            >>   53 JUMP_ABSOLUTE           13
            >>   56 POP_BLOCK
            >>   57 LOAD_CONST               0 (None)
                 60 RETURN_VALUE
    >>> def Generator_expr(x, y):
    ...    return ((i, j) for i in xrange(x) for j in xrange(y))
    ...
    >>> dis.dis(Generator_expr.func_code.co_consts[1])
      2           0 SETUP_LOOP              47 (to 50)
                  3 LOAD_FAST                0 (.0)
            >>    6 FOR_ITER                40 (to 49)
                  9 STORE_FAST               1 (i)
                 12 SETUP_LOOP              31 (to 46)
                 15 LOAD_GLOBAL              0 (xrange)
                 18 LOAD_DEREF               0 (y)
                 21 CALL_FUNCTION            1
                 24 GET_ITER
            >>   25 FOR_ITER                17 (to 45)
                 28 STORE_FAST               2 (j)
                 31 LOAD_FAST                1 (i)
                 34 LOAD_FAST                2 (j)
                 37 BUILD_TUPLE              2
                 40 YIELD_VALUE
                 41 POP_TOP
                 42 JUMP_ABSOLUTE           25
            >>   45 POP_BLOCK
            >>   46 JUMP_ABSOLUTE            6
            >>   49 POP_BLOCK
            >>   50 LOAD_CONST               0 (None)
                 53 RETURN_VALUE
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 506k
  • Answers 506k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer bool ispowerof2(unsigned int x) { return x && !(x &… May 16, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer at face value, the following will work: preg_match('~<([^>]+)>~',$string,$match); but i… May 16, 2026 at 3:46 pm
  • Editorial Team
    Editorial Team added an answer You can use whatever you want. There's no best practice… May 16, 2026 at 3:46 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Is there any difference between: if foo is None: pass and if foo ==
I'm creating one-click python installer (integrated with my application). Is there any way to
Is there any way I can stop python.exe from closing immediately after it completes?
Is there any approach to generate editor of an XML file basing on an
Suppose I have a Python class that I want to add an extra property
I've seen two ways to create an infinite loop in Python: while 1: do_something()
i am quite new to python.subprocess() if i folk a new process from python,
Apologies, somewhat confused Python newbie question. Let's say I have a module called animals.py
In Python the interface of an iterable is a subset of the iterator interface
I need to log the current windows version in my python application for reporting

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.