<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">How does the __del__ method have a reference to the module’s globals dict? because it references the print function?<div class=""><br class=""></div><div class="">Crazy. Is there any other way to comfort when a module is being deleted beside defining a class object with a printing dtor?<br class=""><div class=""><br class=""></div><div class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Nov 23, 2014, at 4:27 PM, Ian Kelly <<a href="mailto:ian.g.kelly@gmail.com" class="">ian.g.kelly@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><p dir="ltr" class=""><br class="">
On Nov 23, 2014 4:10 AM, "Patrick Stinson" <<a href="mailto:patrickkidd@gmail.com" class="">patrickkidd@gmail.com</a>> wrote:<br class="">
> m = types.ModuleType('mine')<br class="">
> exec(s, m.__dict__)<br class="">
> print('deleting...')<br class="">
> m = None<br class="">
> print('done')<br class="">
><br class="">
> and the output is:<br class="">
><br class="">
> deleting...<br class="">
> done<br class="">
> __del__<br class="">
><br class="">
> I the “__del__" to come between “deleting…” and “done”. This is not being run from the interactive interpreter by via a .py file.</p><p dir="ltr" class="">This suggests the presence of a reference cycle, since the object is deleted afterward by the garbage collector. One cycle here is that the __del__ function has a reference to the module's globals dict, which has a reference to the class instance, which has a reference to the class, which has a reference back to the function. There may be other cycles here as well, but it may also be that the module object itself isn't part of them.</p>
-- <br class=""><a href="https://mail.python.org/mailman/listinfo/python-list" class="">https://mail.python.org/mailman/listinfo/python-list</a><br class=""></div></blockquote></div><br class=""></div></div></body></html>