4 ms·
Does anyone know why Python chose linked list for its frames vs an array like Ruby? I imagine both choices have their own advantages and disadvantages.
by hangonhn 5y ago
Does anyone know why Python chose linked list for its frames vs an array like Ruby? I imagine both choices have their own advantages and disadvantages.
- the_mitsuhiko 5y agoMost likely because it was easier. It's definitely not a good choice but unfortunately `PyFrame_GetBack(PyFrameObject *)` exists as an API which makes it tricky now to switch to something else as it must be possible to discover the previous frame from a frame. It's even exposed to userspace with `frameobject.f_back`.
- dmw_ng 5y agoNo definitive answer, but educated guess: I'm not sure how things are in Ruby, but in Python frame object lifetime does not correspond to the C stack implementing the interpreter. If your code throws an exception, frames are returned as part of the public API of the exception object, and can thus indefinitely outlive the associated function execution. You could still implement an array-oriented frame stack in Python, but I think that would mean exceptions become more expensive, as it means making a complete copy of the array any time an exception was thrown, even if the user never makes use of it.
- the_mitsuhiko 5y agoIt would be trivial to change from a linked list to an allocated vector if it were not for exposed APIs. In fact it would most likely be more efficient.
- gpderetta 5y agoInteresting. Wasn't the selling point of Stackless Python that it did not use the C stack (as done by CPython) as the Python stack?