On Sun, Aug 6, 2017 at 1:23 AM, Tim Daneliuk <i...@tundraware.com> wrote: > On 08/04/2017 07:00 PM, Chris Angelico wrote: >> Again, don't stress about exactly when objects get >> disposed of; it doesn't matter. > > > Respectfully, I disagree strongly. Objects get build on the heap and > persist even when they go out of scope until such time garbage > collection takes place. This is unlike languages that build things in > stack frames which naturally disappear with an exit of scope. > > For small or trivial programs, it does not matter. But when there is a > lot of dynamic object construction - say, in very large programs, object > factories, etc. - it can be important to harvest the space of expired > objects sooner, rather than later. This, after all, is one of the > rationale' for Python contexts - to ensure the release of resources no > matter how the logic ends - correctly or by exception.
By "contexts", you're presumably talking about the way you can use a 'with' block to guarantee resource release. But that is actually orthogonal to object destruction; in fact, it's specifically because the object might NOT be destroyed at that point. Before that feature was implemented, people depended on CPython's reference counting and consequent object destruction (and __del__) to close files and other resources. That doesn't work reliably in all Python implementations, though, so a more dependable system was needed. After a 'with' block, the object *still exists*, but it has been "exited" in some way (usually by closing/releasing an underlying resource). So, again, you must not concern yourself with when the objects themselves get destroyed. If there's a resource you need to clean up, you clean that up explicitly, so the object's lifetime shouldn't matter to you. ChrisA -- https://mail.python.org/mailman/listinfo/python-list