[Python-Dev] With statement

Moore, Paul Paul.Moore@atosorigin.com
Wed, 5 Feb 2003 09:49:51 -0000


From: Alex Martelli [mailto:aleax@aleax.it]
> Indeed, why _shouldn't_ file do the obvious thing?  Just
> like it has a __del__ =3D close, so should it have an __exit__
> (the latter only if with IS introduced, of course).

I've added a note to the PEP. The main disadvantage I can see
is that if file objects have __exit__ =3D close, then people will
write code which uses "with myfile". That code then won't work
with a StringIO object (unless StringIO grows an __exit__
method). OK, so we add it to StringIO. But my point is that
__exit__ =3D close becomes an important part of the "file-like
object" protocol.

I don't have a strong opinion. I've left it as an open issue.
(Actually, I do have a strong opinion - I don't want to add
more requirements to the already-vague file object protocol,
just for the sake of convenience. But I don't want to spend
time arguing over it :-))

Paul