[Python-ideas] Automatic context managers

Andrew Barnert abarnert at yahoo.com
Sat Apr 27 05:35:49 CEST 2013


On Apr 26, 2013, at 8:21, anatoly techtonik <techtonik at gmail.com> wrote:

> On Fri, Apr 26, 2013 at 4:55 PM, Ethan Furman <ethan at stoneleaf.us> wrote:
>> On 04/26/2013 06:02 AM, 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:
>>> 
>>>     On 25/04/13 14:47, anatoly techtonik wrote:
>>> 
>>>         On Thu, Apr 25, 2013 at 7:23 AM, Cameron Simpson <cs at zip.com.au <mailto:cs at zip.com.au>> wrote:
>>> 
>>> 
>>>             Then aren't you just talking about the __del__ method?
>>> 
>>> 
>>>         No. The __del__ method is only called during garbage collection phase which
>>>         may be delayed. In PySide the QObject is deleted immediately.
>>> 
>>> 
>>>     Citation please. Where is this documented?
>>> 
>>> 
>>> Here:Â http://qt-project.org/wiki/PySide_Pitfalls
>>> 
>>> 
>>> """
>>> 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’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.

You're missing something fundamental here. 

PySide isn't advertising that you can automatically clean up objects just by leaking them; it's warning you that you must retain objects if you don't want them cleaned up. 

Put another way: correct Python code cannot assume that objects _do_ outlive their last reference... But that doesn't mean you can assume they _don't_, either. You have to assume that both are possible, and code accordingly.

And there is really no obvious change to the language that would "fix" that in either direction.

Or, rather, there is: make it easier to do the right thing explicitly. Which is exactly what with statements are for.

> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> http://mail.python.org/mailman/listinfo/python-ideas
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20130426/11c132f3/attachment.html>


More information about the Python-ideas mailing list