[Python-Dev] A Hygienic Macro System in Python?
Duncan Booth
duncan@rcp.co.uk
Tue, 19 Mar 2002 12:20:45 +0000
On 18 Mar 2002, Skip Montanaro <skip@pobox.com> wrote:
> I think a "lock" keyword would be
> appropriate:
>
> lock somelock:
> do stuff
>
> The biggest problem I found with the try/finally lock idiom was that
> "do stuff" can tend to get long, so the vertical distance between
> lock.acquire() and lock.release() can be substantial. a lock
> statement/clause/macro would remove the need to worry about that
> visual distance.
This is one of the areas that I think Microsoft got right in C#. C# has a
using statement:
using (resource-acquisition) embedded-statement
where resource-acquisition is a local variable declaration or a expression
that creates an object with an IDisposable interface.
The using statement effectively wraps a try..finally around the embedded
statement, and the finally calls the Dispose method of the object created
in the resource-acquisition part.
I guess a direct translation into Python would be:
using assignment_stmt: suite
and:
using expression: suite
Which would be roughly equivalent to:
__usingvar__ = assignment_stmt (or expression)
try: suite
finally:
if isinstance(__usingvar__, tuple):
for item in __usingvar__: item.Destroy()
else:
__usingvar__.Destroy()
--
Duncan Booth duncan@rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?