[Python-ideas] Automatic context managers

Ned Batchelder ned at nedbatchelder.com
Fri Apr 26 17:39:24 CEST 2013


On 4/26/2013 11:21 AM, anatoly techtonik wrote:
>
>         """
>         If a QObject falls out of scope in Python, it will get
>         deleted. You have to take care of keeping a reference to the
>         object:
>
>         * Store it as an attribute of an object you keep around, e.g.
>         self.window = QMainWindow()
>         * Pass a parent QObject to the objectâEUR^(TM)s constructor,
>         so it gets owned by the parent
>
>         """
>
>             This thread on the PySide mailing list suggests that you
>         are mistaken, PySide does not have superpowers over and
>             above Python's garbage collector, and is subject to the
>         exact same non-deterministic destructors as any other Python
>             object. Whether you call that destructor __del__ or
>         __exit__ makes no difference.
>
>         http://www.mail-archive.com/__pyside@lists.openbossa.org/__msg01029.html
>            
>         <http://www.mail-archive.com/pyside@lists.openbossa.org/msg01029.html>
>
>
>     You'll notice it doesn't say "gets /immediately/ deleted" --
>     because it doesn't.  It gets deleted when it gets garbage collected.
>
>
> Are you sure about that? The example on the PySide wiki is pretty 
> reproducible. With current garbage collector lazyness it should be at 
> least in some cases non-reliable.

Again, I suspect we are falling prey to fuzzy language.  CPython will 
reclaim objects as soon as their reference count reaches zero. This is 
not the garbage collector.  The garbage collector is a separate facility 
which kicks in every once in a while to find objects that have non-zero 
reference counts, even though they are unreachable, because of circular 
references.  Some people say "garbage collector" or "garbage collection" 
to mean the usual reclamation of objects when their refcount reaches 
zero, but this is imprecise and confusing when mixed with people who use 
the term differently.

I know nothing about the internals of QObjects, but like most others, I 
*strongly* suspect that they are doing nothing special above and beyond 
what Python does to determine the lifetime of an object.  Their cleanup 
happens when the object is reclaimed (note I am careful not to say, 
"when the object is garbage collected").

The example on the PySide wiki is reproducible because it is not subject 
to garbage collector laziness.  The "animation" name is a local in 
animate_stuff.  At the end of that function, the name "animation" falls 
out of scope, decrementing the reference count on the QPropertyAnimation 
object it referenced.  That object now has a reference count of zero, so 
it is reclaimed.  The cleanup code is then invoked, destroying the 
native objects as well.

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


More information about the Python-ideas mailing list