I'm not familiar with Python since I'm from Java background. In Java throwing exceptions is costly (like null check is preferred over catching NPE) since exception needs to get stacktrace which involves quite a lot of reflection.
Is this the case with python too? or throwing exception is "free" here?
Sort of. In Python the stacktrace is always built for every step you take in code, so exceptions are not particularly costly compared to other operations. Reflection is also not relatively more costly since types are objects just like everything else.
It could even be more efficient than return value checking in certain cases.
uranusjr|7 years ago
It could even be more efficient than return value checking in certain cases.