On Tue, 11 Mar 2025, RAGINI wrote:


 The latter is a 'compound' statement!

 the 'with' statement allows the execution of
 - initialization code
 - finalization code
 around a block of code


Out of the two which one would you recommend that as a community we
should use?


Checking in Python 3.14, there is 'contextlib' module which contains utilities 
to work with 'with-statement' contexts.

abc.ABC(builtins.object)
        AbstractAsyncContextManager
            AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager)
            aclosing
        AbstractContextManager
            ExitStack(_BaseExitStack, AbstractContextManager)
            chdir
            closing
            nullcontext(AbstractContextManager, AbstractAsyncContextManager)
            suppress
    builtins.object
        ContextDecorator
    _BaseExitStack(builtins.object)
        AsyncExitStack(_BaseExitStack, AbstractAsyncContextManager)
        ExitStack(_BaseExitStack, AbstractContextManager)
    _RedirectStream(AbstractContextManager)
        redirect_stderr
        redirect_stdout

Thus, 'with' is the idiomatic way to write code containing compound statements.


warm regards
Saifi.

Reply via email to