3 ms·
I agree that the recursive solution is nice and simple. We've also used it at work. However, you can run into "maximum recursion depth exceeded" issues if the
by freininghaus 6y ago
I agree that the recursive solution is nice and simple. We've also used it at work.
However, you can run into "maximum recursion depth exceeded" issues if the number of context managers is extremely large. ExitStack handles this situation nicely [1].
Note that I'm not saying that using so many context managers is a good idea ;-) In our case, using ExitStack (which also has a backport to Python 2 [2]) was the simplest fix though.
[1] https://github.com/freininghaus/notes/blob/master/misc-notebooks/2020-11-11-python-exitstack-vs-recursion.ipynb https://github.com/freininghaus/notes/blob/master/misc-noteb... or https://nbviewer.jupyter.org/github/freininghaus/notes/blob/master/misc-notebooks/2020-11-11-python-exitstack-vs-recursion.ipynb https://nbviewer.jupyter.org/github/freininghaus/notes/blob/...
[2] https://contextlib2.readthedocs.io/en/stable/#contextlib2.ExitStack https://contextlib2.readthedocs.io/en/stable/#contextlib2.Ex...