[Python-ideas] Block-Scoped Exception Handlers

Kyle Lahnakoski klahnakoski at mozilla.com
Thu May 5 15:17:43 EDT 2016


On 5/5/2016 12:04 PM, Paul Moore wrote:
> On 5 May 2016 at 16:35, Kyle Lahnakoski <klahnakoski at mozilla.com> wrote:
>> `raise from` does not solve the excessive indentation problem:  I have many
>> `try` clauses, causing deep indentation in my code.  The block-scoped
>> exception handlers would mitigate this deep indentation, and make exception
>> handling even easier to add.
> contextmanager/ExitStack would likely help with the indentation
> problem. As would simply breaking out some of the deeply nested code
> into independent functions.
>
May you provide me with an example of how contextmanager would help with
the indentation?  From what little I can glean, Python2.7 already has
this, and I use it, but I do not see how replacing `try` blocks with
`with` blocks reduces indentation.   I do agree it looks cleaner than a
`try/except` block though.  

I do agree that breaking out deeply nested code into independent
functions can help with the indentation.  I find breaking out functions
that only have call site quite disappointing.  My disappointment is
proportional to the number of block-scoped variables.


Thank you!



More information about the Python-ideas mailing list