[Python-ideas] Potential PEP: with/except

Steven D'Aprano steve at pearwood.info
Thu Jan 31 01:39:10 EST 2019


On Wed, Jan 30, 2019 at 08:27:41PM -0600, Abe Dillon wrote:

> I've heard there's a non-insignificant performance penalty for setting up a
> try statement, so it might be important to only set a for-loop up as a
> guarded for-loop upon reading the "except" statement (if the compiler can
> handle such behavior).

I believe you have been misinformed. I admit I haven't tried it 
recently, but back in Python 2.5 days or so I ran some benchmarks which 
satisfied me that:

- the cost of setting up a try block was effectively zero;
- the cost of catching an exception is quite steep.

I'd be surprised if Python 3 reverses that.


-- 
Steven


More information about the Python-ideas mailing list