Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are generators supported in RPython?

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.

like image 448
costy.petrisor Avatar asked Jun 08 '11 10:06

costy.petrisor


People also ask

Are generators useful in Python?

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.

How do generators work in Python?

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.

Are generators lazy in Python?

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.

Is Python a generator range?

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.


1 Answers

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.

like image 121
fijal Avatar answered Oct 26 '22 20:10

fijal