[Python-Dev] Acquire/release functionality

Alex Martelli aleax@aleax.it
Mon, 3 Feb 2003 16:11:08 +0100


On Monday 03 February 2003 03:31 pm, Moore, Paul wrote:
   ...
>     # File now gets closed
>     with g:
>        # Who's a naughty boy then - g is a closed file
>     # And the g.__exit__() call goes boom here...

Why does it go boom?

> I don't know if this is a significant issue - after all, I can write
> code which closes a closed file object in normal Python. But I do think

Sure:

>>> f=file('noproblemo','w')
>>> f.close()
>>> f.close()
>>> f.close()
>>>

...and it gives absolutely no problem.  We can close it a few more
times too.  I *LIKE* this: a termination-method SHOULD be idempotent
(a no-op when called more than once), so that if various ways to
"ensure the termination method is called" happen to be used all
at once, that doesn't break anything.  In this case, I think convenience
should trump "errors shouldn't pass silently" -- i.e. I'd much rather
define multiple calls to the terminator as "not an error", just like the
built-in file object does!

> If nothing else, it strikes me as a mild argument in favour of allowing
> the assignment to be part of the "with" clause, just so that people
> don't get used to seeing a bare "with g" (which doesn't include any
> obvious clues that g *needs* auto-cleanup).

Heh, good try (and I *WOULD* definitely like it if the optional assignment
could be part of the with syntax, since I think there will be an assignment
needed more often than not), but I'm not sure this specific argument
is that strong a support for our shared preference;-).


Alex