Nov-02-2017, 08:45 PM
Generators make a lot of things very easy in python that would be much harder in a lot of other languages. Consider a simple counter:
def counter(start_value=0, increment_by): value = start_value while True: yield value value += increment_byIn another language, such as c# or java, the easiest way to do that would probably be creating a whole class for it, to maintain state, and have a
next_value
method. That said, I think those languages have been adding yield features, so it may not be as true as it once was. Newer versions of javascript, for example, would look very similar to the python above (except with braces, obviously). But then you run the issue of your code sometimes not working, as yield was added in Firefox in 2013, but has never worked in IE (reference: http://caniuse.com/#feat=es6-generators).