[Python-Dev] A Hygienic Macro System in Python?

Duncan Booth duncan@rcp.co.uk
2002年3月19日 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円\x83円"
"6円7円\xb1円\x9\xa2円0円4円"];} // Who said my code was obscure?

AltStyle によって変換されたページ (->オリジナル) /