context managers inline?
Ian Kelly
ian.g.kelly at gmail.com
Thu Mar 10 14:19:19 EST 2016
On Thu, Mar 10, 2016 at 11:59 AM, Neal Becker <ndbecker2 at gmail.com> wrote:
> sohcahtoa82 at gmail.com wrote:
>
>> On Thursday, March 10, 2016 at 10:33:47 AM UTC-8, Neal Becker wrote:
>>> Is there a way to ensure resource cleanup with a construct such as:
>>>
>>> x = load (open ('my file', 'rb))
>>>
>>> Is there a way to ensure this file gets closed?
>>
>> with open('my file', 'rb') as f:
>> x = load(f)
>
> But not in a 1-line, composable manner?
def with_(ctx, func):
with ctx as value:
return func(value)
x = with_(open('my file', 'rb'), load)
Seems less readable to me, though.
More information about the Python-list
mailing list