Jeff Shannon wrote: > Christopher J. Bottaro wrote: > >> 2 Questions... >> 1) Why does this never happen in C++? Or does it, its just never >> happened to me? >> 2) I can understand random destruction of instantiated objects, but I >> find it weird that class definitions (sorry, bad terminology) are >> destroyed at >> the same time. So __del__ can't safely instantiate any classes if its >> being called as a result of interpreter shutdown? Boo... > > Keep in mind that in Python, everything is a (dynamically created) > object, including class objects. My recall of C/C++ memory > organization is pretty weak, but IIRC it gives completely different > behavior to code, stack objects, and heap objects. Code never needs > to be cleaned up. In Python, everything (including functions and > classes) is effectively a heap object, and thus functions and classes > can (and indeed must) be cleaned up. Refcounting means that they > won't ever (normally) be cleaned up while they're still in use, but > during program shutdown refcounting necessarily ceases to apply. > > The closest that would happen in C++, I believe, would manifest itself > as memory leaks and/or access of already-freed memory.
So encapsulating your script in a main function fixes the problem: def main(): blah blah if __name__ == "__main__": main() because all the objects instantiated in main() will be deleted when main ends, but before the interpreter shuts down, thus the objects will have access to all the symbols in module's __dict__ (or however that works). Why can't Python kind of do this step for us? I'm just curious, this stuff is interesting to me. Is it because it wouldn't know what parts of the module should be automatically put in the main function and what parts shouldn't? class blah: pass class blah1: pass # following goes in automatically generated main function obj = blah() obj2 = blah2() obj.doStuff() # main ends # interpreter shutdown I guess it wouldn't know to leave the class objects outside of the automatically generated main function. Just tell if my ramblings are idiotic and I'll shutup. Thanks, -- C -- http://mail.python.org/mailman/listinfo/python-list