Re: [Python-ideas] Nested with statements

pointed out, the main problem was the early evaluation of context managers ; maybe a solution would be to delay the creation of context managers, with something like a partial application (cf functools). Roughly, we'd need a "delayedNested" function, which takes zero-argument callables as parameters, and calls/instanciates them inside itself. Then just call* delayedNested(partial(A,...arguments...), partial(B, ...arguments...))*/ /to have what you want. Yes I know, it's not pretty (mainly because of the lack of partial application syntax in python), but it's just a placeholder ^^ Regards, Pascal

Mathias Panzenböck wrote:
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

Mathias Panzenböck wrote:
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
participants (3)
-
Carl Johnson
-
Mathias Panzenböck
-
Pascal Chambon