[Python-Dev] generalized resource alloc and de-alloc (original) (raw)

Greg Ewing greg@cosc.canterbury.ac.nz
Wed, 20 Mar 2002 12:36:24 +1200 (NZST)


Gareth McCaughan <gmccaughan@synaptics-uk.com>:

with LHS=EXPR: BODY

is the same as TEMPNAME=EXPR LHS=TEMPNAME.begin() try: BODY finally: TEMPNAME.end()

I like the simplicity of this as compared to the "under" proposal.

Here's an even simpler version:

with LHS = EXPR: BODY

is equivalent to

LHS = EXPR try: BODY finally: del LHS

with TEMPNAME substituted for LHS if the latter is is omitted.

As a bonus, this would double as a means of introducing a local name with restricted scope.

In fact, you could turn it around and describe it as a facility for introducing local names with restricted scope, with one application of using it as a resource-acquisition control structure.

Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+