Fix Python – If range() is a generator in Python 3.3, why can I not call next() on a range?

Perhaps I’ve fallen victim to misinformation on the web, but I think it’s more likely just that I’ve misunderstood something. Based on what I’ve learned so far, range() is a generator, and generators can be used as iterators. However, this code:
myrange = range(10)

gives me this error:
TypeError: ‘range’ object is not an it….

Fix Python – How to define an empty generator function?

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():

I could….

Fix Python – How to print a generator expression?

In the Python shell, if I enter a list comprehension such as:
>>> [x for x in string.letters if x in [y for y in “BigMan on campus”]]

I get a nicely printed result:
[‘a’, ‘c’, ‘g’, ‘i’, ‘m’, ‘n’, ‘o’, ‘p’, ‘s’, ‘u’, ‘B’, ‘M’]

Same for a dictionary comprehension:
>>> {x:x*2 for x in range(1,10)}
{1: 2, 2: 4, 3: 6, 4: 8, 5: 10, 6: 12, 7: 14, 8: 16….

Fix Python – Equivalent C++ to Python generator pattern

I’ve got some example Python code that I need to mimic in C++. I do not require any specific solution (such as co-routine based yield solutions, although they would be acceptable answers as well), I simply need to reproduce the semantics in some manner.
This is a basic sequence generator, clearly too large to store a materialized version.