I understand how an object can be extended to be also used as a context manager. I also understand how one can easily create a context manager from a (generator) function using contextlib
.
However, is it possible to write a function that would behave as a function when used as a function,
foo = make_foo()
and as a context manager when used as a context manager,
with make_foo() as foo:
...
Looking around, I would say that this is not possible -- however isn't that precisely the behavior of the open
function? Is this behavior only possible for builtin functions, or can it also be achieved with python code?