[Python-ideas] Nested with statements

Pascal Chambon chambon.pascal at wanadoo.fr
Tue Apr 28 21:59:25 CEST 2009


> spir a écrit :
>> Le Mon, 27 Apr 2009 20:54:58 +0100,
>> Arnaud Delobelle <arnodel at 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 at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20090428/e1256609/attachment.html>


More information about the Python-ideas mailing list