Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #104538 > unrolled thread
| Started by | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| First post | 2016-03-10 13:33 -0500 |
| Last post | 2016-03-11 15:35 +1100 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.python
context managers inline? Neal Becker <ndbecker2@gmail.com> - 2016-03-10 13:33 -0500
Re: context managers inline? sohcahtoa82@gmail.com - 2016-03-10 10:47 -0800
Re: context managers inline? Neal Becker <ndbecker2@gmail.com> - 2016-03-10 13:59 -0500
Re: context managers inline? Ian Kelly <ian.g.kelly@gmail.com> - 2016-03-10 12:19 -0700
Re: context managers inline? jmp <jeanmichel@sequans.com> - 2016-03-11 11:42 +0100
Re: context managers inline? Steven D'Aprano <steve@pearwood.info> - 2016-03-11 15:35 +1100
| From | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| Date | 2016-03-10 13:33 -0500 |
| Subject | context managers inline? |
| Message-ID | <mailman.144.1457634813.15725.python-list@python.org> |
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?
[toc] | [next] | [standalone]
| From | sohcahtoa82@gmail.com |
|---|---|
| Date | 2016-03-10 10:47 -0800 |
| Message-ID | <5474fede-5093-4a0b-8e1e-22737e6e7810@googlegroups.com> |
| In reply to | #104538 |
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)
[toc] | [prev] | [next] | [standalone]
| From | Neal Becker <ndbecker2@gmail.com> |
|---|---|
| Date | 2016-03-10 13:59 -0500 |
| Message-ID | <mailman.146.1457636383.15725.python-list@python.org> |
| In reply to | #104540 |
sohcahtoa82@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?
[toc] | [prev] | [next] | [standalone]
| From | Ian Kelly <ian.g.kelly@gmail.com> |
|---|---|
| Date | 2016-03-10 12:19 -0700 |
| Message-ID | <mailman.147.1457637602.15725.python-list@python.org> |
| In reply to | #104540 |
On Thu, Mar 10, 2016 at 11:59 AM, Neal Becker <ndbecker2@gmail.com> wrote:
> sohcahtoa82@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.
[toc] | [prev] | [next] | [standalone]
| From | jmp <jeanmichel@sequans.com> |
|---|---|
| Date | 2016-03-11 11:42 +0100 |
| Message-ID | <mailman.13.1457693002.26429.python-list@python.org> |
| In reply to | #104540 |
On 03/10/2016 07:59 PM, Neal Becker wrote:
> sohcahtoa82@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?
>
You you really want a 1 liner
with open('my file', 'rb') as f: print 'foo'; x = load(f)
jm
[toc] | [prev] | [next] | [standalone]
| From | Steven D'Aprano <steve@pearwood.info> |
|---|---|
| Date | 2016-03-11 15:35 +1100 |
| Message-ID | <56e24b07$0$1586$c3e8da3$5496439d@news.astraweb.com> |
| In reply to | #104538 |
On Fri, 11 Mar 2016 05:33 am, 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?
Depends on what you mean by "ensure". Have load() call the file's close
method may be good enough.
If you want a better guarantee, you need either a with block:
with open(...) as f:
...
or a finally block:
try:
...
finally:
...
There is no expression-based version of these.
--
Steven
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web