[Python-ideas] Nested with statements

Mathias Panzenböck grosser.meister.morti at gmx.net
Wed Apr 29 13:07:52 CEST 2009


Carl Johnson wrote:
 > Mathias Panzenböck wrote:
 >> Pascal Chambon wrote:
 >>> Then just call* delayedNested(partial(A,...arguments...), partial(B,
 >>> ...arguments...))*/ /to have what you want.
 >>>
 >> It would be much shorter and more readable to manually nest the with
 >> statements.
 >
 > You could improve the look a little by changing the input excepted. Say:
 >
 > with delaynested( [A, B], [(A_arg1, A_arg2), B_args]):
 >     do_stuff()
 >
 > A realistic example:
 >
 > with delaynested([getlock, open], [None, ("file.txt",)]): #Notice that
 > the comma is important!
 >     do_stuff()
 >
 > Is that so bad? I think it looks OK.
 >
 > That said, it would be nice if there were a better way to do partial
 > functions than either lambda or functools.partial.
 >
 > -- Carl

Or:
with delaynested((getlock,), (open,"file.txt")) as lock, fp:
     do_stuff()

But what about keyword parameters? The clean solution is the new syntax.

	-panzi



More information about the Python-ideas mailing list