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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:54:41+00:00 2026-05-26T05:54:41+00:00

I was curious about using static variables in python, and ended up at: Why

  • 0

I was curious about using static variables in python, and ended up at: Why doesn't Python have static variables?

In the accepted answer, which I found informative, it is said “If you want your function’s behavior to change each time it’s called, what you need is a generator”.

However I was a bit confused, since the example used there can be done with a class as well:

class Foo(object):                                                              
    def __init__(self, bar):                                                    
        self.bar = bar                                                          

    def __call__(self):                                                         
        self.bar = self.bar * 3 % 5                                             
        return self.bar

foo = Foo(bar)                                                                  
print foo()                                                                     
print foo()

This makes more sense to me (but probably only because I haven’t used generators properly before).

So my question is whether there is some other advantage to using generators over classes when the function needs to change behavior each time it’s called.

  • 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-26T05:54:42+00:00Added an answer on May 26, 2026 at 5:54 am

    Generators (the simple ones, not getting into coroutines) have a single, very specific purpose: Writing iterators. To this end, you get implicit capture of local variable and a keyword to suspend (as opposed to “terminate” – you can resume) execution and give the caller the next value of the iterator. The advantages are:

    • The logic reads more naturally, as you don’t need to chop it into several method invokations – all state is preserved, including the program counter – you can use control flow across yielding . You could replace yield x with results.append(x) and return results at the end and get a function that’s equivalent to list(the_original_generator(...)).
    • You don’t need to explicitly make values long-lived by putting them into self – less to type and less to read.
    • You don’t need a class declaration with two method declarations (a few lines of boilerplate, which is considerable by Python standards).

    You can use the “capture of local variable” thing to implement an iterable which behaves as, except that you can have multiple ones and you don’t invoke a function but use an iterator (next_val = next(vals) instead of next_val = vals()). Whether it is appropriate depends.

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

Sidebar

Related Questions

I was curious about the performance of insert-sort using C and python but the
I'm curious about people's approaches to using stored procedures in a database that is
Joel often talks about using MS Excel for lightweight project management, but I'm curious
I'm new to C#, and am curious about best practice for using namespaces. I
I was curious how anyone would go about creating WCF based services using a
Just curious about the encodings that system is using when handling string storing(if it
Hi Im curious about how DDD is really implemented using Fluent Nhibernate. for example
i'm just curious about something.I'm using hsql in myproject (embedded of course).At some time
I'm using Postgres, but I'm curious about this just in general. People talk about
I am pretty new to using object/classes in PHP and I am curious about

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.