4 ms·
Impossible to avoid in python.. exceptions are even how a simple for loop is implemented under the covers!
by gegtik 4y ago
Impossible to avoid in python.. exceptions are even how a simple for loop is implemented under the covers!
- bushbaba 4y agoPython was never designed to be performance sensitive. Most of python’s high performance libraries were written in C/C++.
- kristianp 4y agoIs that still true in recent Python versions? Sounds like low-hanging fruit for perf optimisation.
- sgtnoodle 4y agoUsing exceptions in python isn't any more expensive than not using exceptions because the interpreter pays the same cost no matter what. It's a core design decision. Changing it would probably break some things.
- stevesimmons 4y agoPython 3.11 made the "try" part of exceptions zero-cost. The "except" part only has overhead if the exception is triggered.
- diarrhea 4y agoSo the answer to the question is yes, because for loops signal their finishing by raising an exception.
- tsimionescu 4y agoYes, the for loop still calls next() on the iterator until it throws a StopIteration exception.