Lifetime of a local reference

Gregory Ewing greg.ewing at canterbury.ac.nz
Thu Feb 28 00:12:57 EST 2019


Alan Bawden wrote:
> the Java Language
> Specification contains the following language:
> 
>    Optimizing transformations of a program can be designed that reduce the
>    number of objects that are reachable to be less than those which would
>    naively be considered reachable.  For example, a Java compiler or code
>    generator may choose to set a variable or parameter that will no longer be
>    used to null to cause the storage for such an object to be potentially
>    reclaimable sooner.

However, it only makes sense to do that if the compiler can be
sure that reclaiming the object can't possibly have any side
effects. That's certainly not true of things like file objects
that reference resources outside of the program. I'd be pretty
upset if a Java implementation prematurely closed my files on
the basis of this clause.

Similar considerations apply to Python. Even more so, because
its dynamic nature makes it next to impossible for the compiler
to prove much of anything about side effects. So I wouldn't
expect a Python implementation to even try to drop any
references early. If it does, I would hope it darn well knows
what it's doing.

Summary: You don't have to worry about it.

-- 
Greg


More information about the Python-list mailing list