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

Michael Hudson mwh at python.net
Tue Aug 26 13:42:49 EDT 2003


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.  For *me*, I think having less
magic is worth the pain of adding "__exit__ = close" to a few class
statements.  Do you want to add "release" as a synonym too?  I don't.

Cheers,
mwh

-- 
  This makes it possible to pass complex object hierarchies to
  a C coder who thinks computer science has made no worthwhile
  advancements since the invention of the pointer.
                                       -- Gordon McMillan, 30 Jul 1998



More information about the Python-Dev mailing list