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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:09:25+00:00 2026-06-12T12:09:25+00:00

Possible Duplicate: Difference between Python Generators vs Iterators Generators seem like big deal in

  • 0

Possible Duplicate:
Difference between Python Generators vs Iterators

Generators seem like big deal in Python, new features are added to them now and then and so on.

As far as I can see, instead generators you could always use an object with iterator interface. Is (usually) better conciseness the only benefit of generators or am I missing something?

  • 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-12T12:09:26+00:00Added an answer on June 12, 2026 at 12:09 pm

    Yes, iterators are a more general construct, and anything you could do with a generator could be done with an iterator.

    However, generators are really nice tool to express certain ideas in a very clean and concise fashion, for which iterators would become cumbersome.

    For example, here’s a simple function:

    def count_to(m):
        n = 0
        while n <= m:
            yield n
            n += 1
    

    Nice and easy. Here’s the same thing as an iterator:

    class CountTo:
        def __init__(self, m):
            self.m = m
            self.n = 0
        def __iter__(self):
            return self
        def next(self):
            if self.n <= self.m:
                cur, self.n = self.n, self.n + 1
                return cur
            else:
                raise StopIteration()
    

    One is 5 lines, the other is 12. The generator expresses the iteration process very succintly, while the iterator obfuscates it with explictly-maintained state and boilerplate code.

    A lot of Python’s philosophy is based around readability and simplicity. In keeping with this, I feel that generators offer a nicer interface for a broad class of tasks that would otherwise require iterators. Yes, iterators are more powerful, but the syntactic advantages of generators certainly can’t be overlooked.

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

Sidebar

Related Questions

Possible Duplicate: Difference between OnClick() event and OnClickListener? I'm semi-new to Android development and
Possible Duplicate: Difference between “var” and “object” in C# I would like to know
Possible Duplicate: difference between throw and throw new Exception() I'm a programmer working on
Possible Duplicate: What is the difference between @staticmethod and @classmethod in Python? I am
Possible Duplicate: In Python, what is the difference between “.append()” and “+= []”? In
Possible Duplicate: Instance variables vs. class variables in Python What is the difference between
Possible Duplicate: Difference Between Equals and == in which cases equals() works exactly like
Possible Duplicate: what is difference between init and call in python? I'm trying to
Possible Duplicate: python import question What is the difference between importing a Python file
Possible Duplicate: C#: Difference between ' += anEvent' and ' += new EventHandler(anEvent)' What

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.