Andrew Dalke wrote: > The implementation would need to track all the with/as forms > in a block so they can be __exit__()ed as appropriate. In this > case ghi.__exit() is called after jkl.__exit__() and > before defg.__exit__ > > The PEP gives an easy-to-understand mapping from the proposed > change to how it could be implemented by hand in the existing > Python. Can you do the same?
I think it is simple and that the implementation is as much straight-forward. Think about it, it just means that: > with abc > > with defg: > with ghi > with jkl: > 1/0 is equivalent to: > with abc: > > with defg: > with ghi: > with jkl: > 1/0 That's it. Nothing more complex. It's only about syntax. > I have not idea if the problem you propose (multiple with/as > blocks) will even exist so I can't comment on which solution > looks good. It may not be a problem in real code, so not needing > any solution. Good point. As a C++ programmer, I use RAII a lot. However, there's situations in C++ that don't apply, like allocating dynamic memory in a scope. However, I still expect some Python programmers to use it enough to ask for that syntax to be added, in the same way operators like += have been added. But there's another point that has nothing to do with how many "with" statements you have in a function. In C++, very very rarely I've seen something like: void foo() { { // (define a scope for the Lock object) Lock locking(myMutex); ... } ... { Lock locking(myMutex); ... } } So I come to another conclusion: the indentation syntax will most of the time result in a waste of space. Typically a programmer would want its with-block to end at the end of the current block. So basically, there's two 10-90% points, one in favor of my proposal, one against: - Most of the time, you don't have a lot of with-statements in a single function, so not so much indentation. - Most of the time, a with-statement ends at the end of current block, so indentation-syntax most of the time result in a waste of space. The way to see my proposal is not "to be used when you have multiple with-blocks" but instead "never use the ':' syntax, unless necessary". The day some code need it, it's very easy to add a ':' and indent some code with our favorite editor. Regards, Nicolas -- http://mail.python.org/mailman/listinfo/python-list