[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Question About When Objects Are Destroyed

On Sat, Aug 5, 2017 at 9:42 AM, Jon Forrest <nobozo at gmail.com> wrote:
> On 8/4/2017 4:34 PM, gst wrote:
>> 'two' is a so called constant or literal value .. (of that
>> function).
>> Why not attach it, as a const value/object, to the function itself ?
>> So that a new string object has not to be created each time the
>> function is called. Because anyway strings are immutable. So what
>> would be the point to recreate such object every time the function is
>> called ?
> This was just an example program, not meant to do anything
> meaningful. I would think that the same object behavior would
> occur if I dynamically created an object in that function.

Python doesn't have pointers, so what you have is an arbitrary number.
Even if the object had been freed from memory, you could quite
probably do the same shenanigans to get a value out of it; the data
would still be there.

Basically, don't think about object lifetimes. Objects will be flushed
from memory once they're not needed any more, and no sooner; so you
can safely return anything from any function.