[Python-Dev] Acquire/release functionality

Moore, Paul Paul.Moore@atosorigin.com
Mon, 3 Feb 2003 15:24:26 -0000


From: Aahz [mailto:aahz@pythoncraft.com]
> It's not at all contrived.  And I think it's perfectly acceptable;
> it's no different, fundamentally, from calling f.close() twice by
> mistake.

I blew the example, as I forgot that multiple close() calls were OK.
And maybe Alex's point that cleanup should be callable multiple times
kills the whole argument.

But I'm not entirely sure I follow your comment. Are you saying that
you think that using the same object in 2 with statements (where, by
definition, in the second the object will be "already cleaned up") is
acceptable, or that you think it's a mistake (in which case, are you
saying that we shouldn't care *because* the coder made a mistake?)

Actually, I think I'm guilty of muddy thinking here, in any case. I
can imagine a single "manager" object, which could be entirely valid
in multiple with statements:

    # Reusing the tired old file closing example, sorry, I'm
    # too befuddled to think up a new example right now...
    class manager:
        def __init__(self, f =3D None):
            self.f =3D f
        def control(self, f):
            self.f =3D f
        def __exit__(self):
            if self.f:
                self.f.close()
            self.f =3D None

    f =3D open("whatever.txt")
    M =3D manager(f)

    with M:
        # use M.f, could expose this better...

    M.control(open("another.txt"))
    with M:
        ...

I really can't decide if this is reasonable use, or horrible abuse. Or
somewhere in between. I'm pretty sure I wouldn't like to have to =
maintain
code like this at short notice, though...

Time to stop responding to messages and have a good think about all of
this.

Paul.