[Python-Dev] With statement

Moore, Paul Paul.Moore@atosorigin.com
Tue, 4 Feb 2003 16:45:28 -0000


From: Duncan Booth [mailto:duncan@rcp.co.uk]
> Your autoclose example doesn't use __enter__, and your autolock =
example=20
> would work just as well if you moved the __enter__ code into __init__=20
> (although then you couldn't write it the alternative way you suggest).

> Do you have any good examples that actually require __enter__, or =
could it=20
> be dropped for even more simplicity?

My actual preference is for a lock object with enter =3D acquire and
leave =3D release. Then, the idiom is

    with my_lock:
        # protected code

This requires a separate enter hook.

Paul.