[Python-ideas] Automatic context managers

Ned Batchelder ned at nedbatchelder.com
Fri Apr 26 15:33:38 CEST 2013


On 4/26/2013 9:22 AM, anatoly techtonik wrote:
> The proposal is fully illustrated by the user story above - 
> immediately close file after it is read operation is complete. The 
> proposed implementation is automatic context manager -- optional, 
> Python level mechanism to run exit part of the context protocol when 
> object loses all references. GC is out of scope here.

The behavior is already that files are closed as part of reclaiming the 
file object.  I don't see how your proposal could change that behavior, 
since your proposal (I think) is to call __exit__ when the object is 
reclaimed, but all that does for a file is close the file.  Since your 
proposal doesn't seem to change existing behavior, I can only conclude 
that either you misunderstand the existing behavior, or I misunderstand 
your proposal.

>     Automaticly calling __exit__ when an object goes out of scope
>     won't work either, it would break passing arguments to functions.
>
>
> Why? Can you provide an example?
>

Part of the confusion here is the phrase "when an object goes out of 
scope".  Values in Python have no scope.  Names have scope.  This 
proposal doesn't involve names, it involves values, and so can have 
nothing to do with scope, unless I've misunderstood something.

--Ned.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130426/8664f087/attachment.html>


More information about the Python-ideas mailing list