05.12.19 04:43, Andrew Barnert via Python-ideas пише:
Yes, you have to unlearn it. Exceptions are not that expensive in Python (and in a lot of other modern languages)—but even if they were, you’d still have to deal with the fact that Python uses them pervasively. Every for loop ends with an exception being thrown and caught, whether you like it or not.
Raising and catching an exception in the C code is much cheaper than in the Python code. In Python you instantiate an exception and set its traceback, context and clause, in C you can just store several threadlocal pointers (most of them NULLs). In python you execute complex bytecode to catch an exception, in C you just read and compare few pointers in common case. StopIteration is not even raised in most case internally in the C code, it is raised only when leaked to the Python code. So exceptions are still expensive in Python, even comparing with other Python code. But outside of tight loops or if you do some input/output, they are proper tool for control flow.