Lifetime of a local reference

Chris Angelico rosuav at gmail.com
Tue Feb 26 17:04:38 EST 2019


On Wed, Feb 27, 2019 at 9:00 AM Marko Rauhamaa <marko at pacujo.net> wrote:
> Consider this function:
>
>     def fun():
>         f = open("lock")
>         flock.flock(f, fcntl.LOCK_EX)
>         do_stuff()
>         sys.exit(0)
>
> Question: can a compliant Python implementation close f (and,
> consequently, release the file lock) before/while do_stuff() is
> executed?
>
> I couldn't find an immediate answer in the documentation.

The variable is alive, so the object is alive. Common sense should
make this clear, but there's probably some details in the
documentation if you actually need a citation. The name "f" is part of
the function's locals until that function returns or the name is
explicitly unbound.

ChrisA



More information about the Python-list mailing list