Re: [Python-ideas] Nested with statements
spir a écrit :
Le Mon, 27 Apr 2009 20:54:58 +0100, Arnaud Delobelle <arnodel@googlemail.com> s'exprima ainsi:
with A(), B() as a,b: # Code that uses a and b.
This would translate directly to:
with A() as a: with B() as b: # Code that uses a and b.
There was a discussion about this on this list:
http://mail.python.org/pipermail/python-ideas/2009-March/003188.html
I can't remember the outcome.
There was no clear outcome, for sure ;-)
Except maybe it was stated that with A(), B() as a,b: should rather be spelled with A() as a, B() as b: to reuse the syntax of imports.
Denis
------ la vita e estrany _______________________________________________ Python-ideas mailing list Python-ideas@python.org http://mail.python.org/mailman/listinfo/python-ideas
I agree with the idea of auto-nesting "with", however in the case you
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
Pascal Chambon wrote:
I agree with the idea of auto-nesting "with", however in the case you 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.
It would be much shorter and more readable to manually nest the with statements. -panzi
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
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
participants (3)
-
Carl Johnson
-
Mathias Panzenböck
-
Pascal Chambon