[Python-ideas] Automatic context managers
MRAB
python at mrabarnett.plus.com
Fri Apr 26 17:54:49 CEST 2013
On 26/04/2013 14:02, anatoly techtonik wrote:
> On Thu, Apr 25, 2013 at 9:17 AM, Steven D'Aprano <steve at pearwood.info
> <mailto:steve at pearwood.info>> wrote:
[snip]
> I am not touching destructor methods. The idea is to make with statement
> transparent - embed inside objects that require it. I am not sure what
> the implementation should be. Probably object should have an ability to
> enable context scope tracking in its constructor, to tell Python to call
> its __exit__ method at the moment when its reference count reaches zero,
> and before it is garbage collected.
>
> On the other hand, objects being freed is not deterministic. They'll
> be freed when there are no longer any references to it, which may be
> Never.
>
> Reference counting GCs are deterministic, but cannot deal with
> circular references. Other GCs can deal with circular references,
> but are non-deterministic. Even the Java GC doesn't guarantee that
> the finalize() method will always be called.
>
>
> This circular reference problem is interesting. In object space it
> probably looks like a stellar detached from the visible (attached)
> universe. Is the main problem in detecting it?
>
The problem is in knowing in which order the objects should be
collected.
For example, if A refers to B and B refers to A, should you collect A
then B, or B then A? If you collect A first, then, for a time, B will
be referring to a non-existent object. That's not good if the objects
have destructors which need to be run.
More information about the Python-ideas
mailing list