Phillip J. Eby wrote:
> I'm suggesting that we simply take Nick's proposal to its logical 
> conclusion, and allow any object to be usable under "with", since it 
> does not create any problems to do so.  (At least, none that I can 
> see.)  A redundant 'with' does no harm; in the worst case it's just a 
> hint to the reader about the scope within which an expression is used 
> within the current fuction body.


Do you mean translating this:

   with EXPR1 as VAR1:
       BLOCK1

To something along the lines of:

   the_stmt = EXPR1
   stmt_enter = getattr(the_stmt, "__enter__", None)
   stmt_exit = getattr(the_stmt, "__exit__", None)

   if stmt_enter is None:
       VAR1 = the_stmt
   else:
       VAR1 = stmt_enter()

   if stmt_exit is None:
       BLOCK1
   else:
       exc = (None, None, None)
       try:
           try:
               BLOCK1
           except:
               exc = sys.exc_info()
               raise
       finally:
           stmt_exit(*exc)


It has a certain elegance - you can switch from using an object that needs 
finalisation to one that doesn't without having to change your code.

And library code can safely ensure finalisation without having to check whether 
the object needs it or not - that check becomes an inherent part of the with 
statement.

I'm ambivalent about this one - I see some benefit to it, but there's something 
niggling at the back of my brain that doesn't like it (nothing I can point to 
in 
particular, though).

Cheers,
Nick.

-- 
Nick Coghlan   |   [EMAIL PROTECTED]   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.blogspot.com
_______________________________________________
Python-Dev mailing list
Python-Dev@python.org
http://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
http://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to