Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Is that still true in recent Python versions? Sounds like low-hanging fruit for perf optimisation.


Yes, the for loop still calls next() on the iterator until it throws a StopIteration exception.


Using 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.


Python 3.11 made the "try" part of exceptions zero-cost. The "except" part only has overhead if the exception is triggered.


So the answer to the question is yes, because for loops signal their finishing by raising an exception.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: