"with" should be deprecated with extreme prejudice

Leandro Lucarella llucax at gmail.com
Mon May 18 12:41:56 PDT 2009


Nick Sabalausky, el 18 de mayo a las 15:23 me escribiste:
> "Leandro Lucarella" <llucax at gmail.com> wrote in message 
> news:20090518141908.GB9277 at burns.springfield.home...
> > bearophile, el 18 de mayo a las 04:33 me escribiste:
> >> Andrei, I agree that "with" is dangerous when it shadows outer names
> >> (Python designers have not added a "with" because of this).
> >
> > They did, but with different semantics =)
> > It's used for RAII (I guess you already know that, but maybe other people
> > don't).
> >
> 
> You mean like C#'s "using"? 

I don't know C#'s "using" =)

In Python, with calls a special method at the begining and some other
special method at the end of the block:

with lock:
	do_something()

is like:

lock.__enter__()
try:
	do_something
finally:
	lock.__exit__()

Or something like that. I think you can use a special method to be called
if an exception was raised (similar to scope(exit|failure|success)).

You can also do something like:

with file("some file") as f:
	x = f.read()

to define an alias to a "temporary" object, so you can work with it.
f.__enter__() does nothing and f.__exit__() closes the file.

This is what I remember, for a full, authoritative, description:
http://www.python.org/doc/2.5/whatsnew/pep-343.html

-- 
Leandro Lucarella (luca) | Blog colectivo: http://www.mazziblog.com.ar/blog/
----------------------------------------------------------------------------
GPG Key: 5F5A8D05 (F8CD F9A7 BF00 5431 4145  104C 949E BFB6 5F5A 8D05)
----------------------------------------------------------------------------



More information about the Digitalmars-d mailing list