Re: [Python-ideas] With expressions

2018-08-02 Thread Thomas Nyberg via Python-ideas

On 08/02/2018 12:43 PM, Paul Moore wrote:

But if someone wanted to raise a doc bug suggesting that we mention
this, I'm not going to bother objecting...
Paul



I opened a bug here:

https://bugs.python.org/issue34319

We can see what others think.

Cheers,
Thomas
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] With expressions

2018-08-02 Thread Thomas Nyberg via Python-ideas

On 08/02/2018 12:43 PM, Paul Moore wrote:

I'm not sure I see why you think it wouldn't - opening and closing the
file is a purely internal detail of the function. In any case, you
don't get given a file object, so how could anything *other* than the
read_text() close the file? So you're basically asking "does
Path.read_text() have a bug that causes it to leak a filehandle?" to
which my answer would be "I assume not, until someone demonstrates
such a bug".


To me the following look the same:

Path('file').read_text()
open('file').read()

The first presumably creates a Path object while the second creates a 
file object. Why should I assume that the Path object closes the 
underlying file object after the method is called? I mean maybe my 
assumption is bad, but I doubt I'd be the only one making it given how 
open() works and that it looks similar superficially.


Cheers,
Thomas
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


Re: [Python-ideas] With expressions

2018-08-02 Thread Thomas Nyberg via Python-ideas
Is it true that Path('file').read_text() closes the file after the read? 
I think that is the sort of functionality that Ken is asking for.
It's not clear to me by your linked documentation that it does. If it 
does, maybe that should be made more clear in that linked documentation? 
(Of course, maybe it's written there somewhere and I'm just blind...)


Cheers,
Thomas

On 08/02/2018 11:53 AM, Paul Moore wrote:

On Thu, 2 Aug 2018 at 10:39, Ken Hilton  wrote:


With expressions allow using the enter/exit semantics of the with statement 
inside an expression context. Examples:

 contents = f.read() with open('file') as f #the most obvious one
 multiplecontents = [f.read() with open(name) as f for name in names] 
#reading multiple files

I don't know if it's worth making the "as NAME" part of the with mandatory in 
an expression - is this a valid use case?

 data = database.selectrows() with threadlock

Where this would benefit: I think the major use case is `f.read() with 
open('file') as f`. Previous documentation has suggested `open('file').read()` 
and rely on garbage collection; as the disadvantages of that became obvious, it 
transitioned to a method that couldn't be done in an expression:


That use case is satisfied by pathlib:

Path('file').read_text()

see https://docs.python.org/3.7/library/pathlib.html#pathlib.Path.read_text

Are there any other use cases? I don't see any real advantage here
other than the non-advantage of being able to write one-liners.
Paul
___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/


___
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/