Are generators supported in RPython, because I just read something in PyPy's documentation that says they are not
PyPy Doc - Coding Guide
They seem easy to be translated to a statically typed language like C because every generation step is generated in function call.
Can someone explain why ? Or shed some more light on the subject. I am currently trying to learn the basics of writing RPython safe code.
Generators have been an important part of Python ever since they were introduced with PEP 255. Generator functions allow you to declare a function that behaves like an iterator. They allow programmers to make an iterator in a fast, easy, and clean way.
A Python generator is a function that produces a sequence of results. It works by maintaining its local state, so that the function can resume again exactly where it left off when called subsequent times. Thus, you can think of a generator as something like a powerful iterator.
Introduced with PEP 255, generator functions are a special kind of function that return a lazy iterator. These are objects that you can loop over like a list. However, unlike lists, lazy iterators do not store their contents in memory.
range is a class of immutable iterable objects. Their iteration behavior can be compared to list s: you can't call next directly on them; you have to get an iterator by using iter . So no, range is not a generator.
Generators are not supported simply because they were not needed at the time. The problem is not really having a roughly equivalent functionality in C, but needing to keep a frame of generator alive. Since RPython frames are translated to C frames, to support full python generators you would need some support for getting C frame and copy it somewhere else, or some equivalent.
This was simply hard/not needed and was not implemented.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With