[Python-Dev] Fwd: PEP 310(with-syntax): close synonym of __exit__

Samuele Pedroni pedronis at bluewin.ch
Tue Aug 26 15:52:41 EDT 2003

At 12:42 26.08.2003 +0100, Michael Hudson wrote:
>Samuele Pedroni <pedronis at bluewin.ch> writes:
> > maybe this is too much DWIMish, but it occurred to me that a further
> > option would be for the semantics to be:
> >
> > var = expr
> >
> > if hasattr(var, "__enter__"):
> >    var.__enter__()
> >
> > try:
> >    suite
> > finally:
> >    if hasattr(var, "__exit__"):
> >      var.__exit__()
> >    elif hasattr(var, "close"): # consider also close as a synonym of 
> __exit__
> >      var.close()
> >
> > I'm not proposing to do without __exit__, I recall the next/__next__
> > debate, but I'm wondering if this is well documented, how many times
> > considering close a synomym of __exit__ would do the wrong thing, such
> > that the user would have to hide close somehow to use 'with' & some
> > object, and this wrt the times someone would need a wrapper to have
> > __exit__ trigger a close.
> >
> > I don't think it is worth to have a debate right now, still this
> > should maybe be added to the PEP as an option to consider.
>FWIW, I think this has come up before.

ok, then the PEP should mention it, or it will come out again.

>   For *me*, I think having less
>magic is worth

if it's documented, I'm not sure its magic,

both the pair __enter__,__exit__

and close alone

seem reasonable protocols for 'with', given the common usage pattern of close.

>the pain of adding "__exit__ = close" to a few class

all file-like objects, HTTP connections, DBI objects (cursors and connections
have close). close is naturally a __exit__

They are not all in the std lib and under our control btw.

>   Do you want to add "release" as a synonym too?  I don't.

No. See above.

Anyway the PEP should mention this, that's what PEPs are for.

Maybe I'm just catering to the lazy, and considering the out-of-the-box 
experience of
the new feature <wink>.


More information about the Python-Dev mailing list