Thread overview | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
February 27, 2018 Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
I know Python's `with` statement can be used to have an automatic close action: ``` with open("x.txt") as file: #do something with file #`file.close()` called automatically ``` I know D's `with` statement does something different but is there some sort of equivalent? |
February 27, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
> I know Python's `with` statement can be used to have an automatic close action:
> ```
> with open("x.txt") as file:
> #do something with file
> #`file.close()` called automatically
> ```
>
> I know D's `with` statement does something different but is there some sort of equivalent?
In this case with(File("bla"))
will do the same.
|
February 27, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote:
> On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
>> I know Python's `with` statement can be used to have an automatic close action:
>> ```
>> with open("x.txt") as file:
>> #do something with file
>> #`file.close()` called automatically
>> ```
>>
>> I know D's `with` statement does something different but is there some sort of equivalent?
>
> In this case with(File("bla"))
> will do the same.
Oh really, cool.
Is this just because the scope of the file variable will end and thus its `~this`?
|
February 27, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan Attachments:
| yes, for classes you can use scoped: import std.stdio; import std.typecons : scoped; class A { void saySomething() { writeln("Hi from A"); } ~this() { writeln("Destruct A"); } } void main() { with(scoped!A()) { saySomething(); writeln("something"); } writeln("Hello D"); } On Tue, Feb 27, 2018 at 5:25 PM, Jonathan via Digitalmars-d-learn < digitalmars-d-learn@puremagic.com> wrote: > On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote: > >> On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote: >> >>> I know Python's `with` statement can be used to have an automatic close >>> action: >>> ``` >>> with open("x.txt") as file: >>> #do something with file >>> #`file.close()` called automatically >>> ``` >>> >>> I know D's `with` statement does something different but is there some sort of equivalent? >>> >> >> In this case with(File("bla")) >> will do the same. >> > > Oh really, cool. > > Is this just because the scope of the file variable will end and thus its `~this`? > > |
February 28, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Stefan Koch | On Tuesday, 27 February 2018 at 16:18:43 UTC, Stefan Koch wrote: > On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote: >> I know Python's `with` statement can be used to have an automatic close action: >> ``` >> with open("x.txt") as file: >> #do something with file >> #`file.close()` called automatically >> ``` >> >> I know D's `with` statement does something different but is there some sort of equivalent? > > In this case with(File("bla")) > will do the same. FWIW std.stdio.File is refcounted and will be automatically closed at the end of the scope. So typically you don't even need `with` ;-) @Jonathan: have a look at these two examples to see what DMD does under the hood: https://run.dlang.io/is/89NBxI https://run.dlang.io/is/eXC7ZV |
February 28, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Jonathan | On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
> I know Python's `with` statement can be used to have an automatic close action:
> ```
> with open("x.txt") as file:
> #do something with file
> #`file.close()` called automatically
> ```
>
> I know D's `with` statement does something different but is there some sort of equivalent?
Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out):
{ // Scope restriction similar to with's block
auto f = File("x.txt");
scope(out) f.close(); // closes f on scope exit no matter what
/* do stuff with f */
}
This accurately reproduces the behaviour of python's with although it's less automatic.
|
February 28, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cym13 | On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote: > On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote: >> [...] > > Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out): > > { // Scope restriction similar to with's block > auto f = File("x.txt"); > scope(out) f.close(); // closes f on scope exit no matter what > > /* do stuff with f */ > } > > This accurately reproduces the behaviour of python's with although it's less automatic. what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement |
February 28, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to meppl | On Wednesday, 28 February 2018 at 22:00:11 UTC, meppl wrote:
> On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:
>> On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
>>> [...]
>>
>> Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out):
>>
>> { // Scope restriction similar to with's block
>> auto f = File("x.txt");
>> scope(out) f.close(); // closes f on scope exit no matter what
>>
>> /* do stuff with f */
>> }
>>
>> This accurately reproduces the behaviour of python's with although it's less automatic.
>
> what is `scope(out)`? I only know these file:///usr/share/dmd-doc/html/d/spec/statement.html#ScopeGuardStatement
sorry, my question was not good. I thought there is an undocumented feature, never mind ;)
|
February 28, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Cym13 | On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:
> On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
>> I know Python's `with` statement can be used to have an automatic close action:
>> ```
>> with open("x.txt") as file:
>> #do something with file
>> #`file.close()` called automatically
>> ```
>>
>> I know D's `with` statement does something different but is there some sort of equivalent?
>
> Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out):
>
> { // Scope restriction similar to with's block
> auto f = File("x.txt");
> scope(out) f.close(); // closes f on scope exit no matter what
>
> /* do stuff with f */
> }
>
> This accurately reproduces the behaviour of python's with although it's less automatic.
I know that I am repeating myself, but manually closing the file isn't needed in D. It's refCounted and will be closed automatically once the scope is left. That's why `with(File("AAA","w"))` works in D.
|
March 01, 2018 Re: Equivalent to Python with Statement | ||||
---|---|---|---|---|
| ||||
Posted in reply to Seb | On Wednesday, 28 February 2018 at 22:55:19 UTC, Seb wrote:
> On Wednesday, 28 February 2018 at 21:47:40 UTC, Cym13 wrote:
>> On Tuesday, 27 February 2018 at 16:17:20 UTC, Jonathan wrote:
>>> I know Python's `with` statement can be used to have an automatic close action:
>>> ```
>>> with open("x.txt") as file:
>>> #do something with file
>>> #`file.close()` called automatically
>>> ```
>>>
>>> I know D's `with` statement does something different but is there some sort of equivalent?
>>
>> Others have discussed that particular case at length, but to provide a more generic answer the correct way to translate a python context manager is to use scope(out):
>>
>> { // Scope restriction similar to with's block
>> auto f = File("x.txt");
>> scope(out) f.close(); // closes f on scope exit no matter what
>>
>> /* do stuff with f */
>> }
>>
>> This accurately reproduces the behaviour of python's with although it's less automatic.
>
> I know that I am repeating myself, but manually closing the file isn't needed in D. It's refCounted and will be closed automatically once the scope is left. That's why `with(File("AAA","w"))` works in D.
As stated, yes that's an accurate answer to that particular problem and it's well understood, I'm just giving a more general answer that will work for every python context manager and not only on the specific case of closing files.
|
Copyright © 1999-2021 by the D Language Foundation