[issue18677] Enhanced context managers with ContextManagerExit and None

Kristján Valur Jónsson report at bugs.python.org
Mon Jan 3 05:35:01 EST 2022


Kristján Valur Jónsson <sweskman at gmail.com> added the comment:

Great throwback.

As far as I know, context managers are still not first class citizens.  You cannot _compose_ two context managers into a new one programmatically in the language, in the same way that you can, for instance, compose two functions.  Not even using "eval()" is this possible.  

This means that the choice of context manager, or context managers, to be used, has to be known when writing the program.  You cannot pass an assembled context manager in as an argument, or otherwise use a "dynamic" context manager at run time, unless you decide to use only a fixed number of nested ones. any composition of context managers becomes syntax _at the point of invocation_.

The restriction is similar to not allowing composition of functions, i.e. having to write

`fa(fb(fc()))` at the point of invocation and not have the capability of doing
```
def fd():
  return fa(fb(fc))
...
fd()
```

I think my "ContextManagerExit" exception provided an elegant solution to the problem and opened up new and exciting possibilities for context managers and how to use them.

But this here note is just a lament.  I've stopped contributing to core python years ago, because it became more of an excercise in lobbying than anything else.
Cheers!

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue18677>
_______________________________________


More information about the Python-bugs-list mailing list