Re: [Python-ideas] With expressions

2018-08-04 Thread Robert Vanden Eynde
> > > > > A with-statement is great for when you care about the > implementation details. Somebody has to care about the process of > opening a file, reading from it and closing it. But when you *don't* > care about those implementation details, a simple interface like a > read() function is superi

Re: [Python-ideas] With expressions

2018-08-04 Thread Robert Vanden Eynde
> I know what functional programming is. What I don't understand is what > you mean when you say that the F.P. community "seems to be more > interested in python". Surely they are more interested in functional > languages than a multi-paradigm language like Python which does not > privilege functio

Re: [Python-ideas] With expressions

2018-08-04 Thread Jeroen Demeyer
On 2018-08-04 09:43, Steven D'Aprano wrote: If you want to propose a general exception suppressing mechanism (aside from the existing try...except statement) then propose it as an independent PEP. This was already tried in PEP 463 (which IMHO is a pity that it wasn't accepted because it seems

Re: [Python-ideas] With expressions

2018-08-04 Thread Steven D'Aprano
On Fri, Aug 03, 2018 at 06:57:40PM -0500, Abe Dillon wrote: > tmp = None > with suppress(AttributeError): > tmp = person.name[0] > initial = tmp > > Then it would solve many of the common use cases for the None-aware > operators proposed in PEP 505 No it would not. The None-aware operators a

Re: [Python-ideas] With expressions

2018-08-04 Thread Steven D'Aprano
On Sat, Aug 04, 2018 at 12:40:13AM +0200, Benedikt Werner wrote: > Overall I like the idea of the with-expression as it allows you to make > some common use cases like the open/read example more readable. For some definition of "readable". > It's > clear at first sight that what is actually d

Re: [Python-ideas] With expressions

2018-08-03 Thread Steven D'Aprano
On Sat, Aug 04, 2018 at 12:28:40AM +0200, Robert Vanden Eynde wrote: > As the code I showed, being just that: > > filename = ... > lines = ... > parsed_data = ... > > With implementation details? The highest value is there, the alg is clear., > one fetches the filename, one preprocess the lines,

Re: [Python-ideas] With expressions

2018-08-03 Thread Steven D'Aprano
On Fri, Aug 03, 2018 at 12:49:24PM +0200, Robert Vanden Eynde wrote: > Le ven. 3 août 2018 à 03:07, Steven D'Aprano a écrit : > > > On Thu, Aug 02, 2018 at 03:13:25PM +0200, Robert Vanden Eynde wrote: > > > > > This brings the discussion of variable assignement in Expression. > > Functional > >

Re: [Python-ideas] With expressions

2018-08-03 Thread Eric Fahlgren
On Fri, Aug 3, 2018 at 5:26 PM Abe Dillon wrote: > One last thing: > > Since expressions tend to have pretty limited space, it might be worth it > to consider more concise options like maybe instead of: > > except (Exception[-list])[ as e]): > You're reinventing PEP 463: https://www.python.or

Re: [Python-ideas] With expressions

2018-08-03 Thread Abe Dillon
One last thing: Since expressions tend to have pretty limited space, it might be worth it to consider more concise options like maybe instead of: except (Exception[-list])[ as e]): it could be: (Exception[-list])[ as e])! So: y = x[0] IndexError! default instead of y = x[0] except Index

Re: [Python-ideas] With expressions

2018-08-03 Thread Abe Dillon
[Benedikt Werner] > As the with-expression mimics the with-statement I would say this is > similar to: > with supress(AttributeError): > tmp = person.name[0] > initial = tmp # Error on assignment wouldn't get suppressed. Not relevant > for this case but still. Ah, yes. That makes much more s

Re: [Python-ideas] With expressions

2018-08-03 Thread Benedikt Werner
For instance, what would the following do? initial = person.name [0] with suppress(AttributeError)  # Hangover from PEP 505 discussion... As the with-expression mimics the with-statement I would say this is similar to: with supress(AttributeError): tmp = person.name[0]

Re: [Python-ideas] With expressions

2018-08-03 Thread Robert Vanden Eynde
> > Expressionization may break the "one and only on obvious way" guideline, > but it can offer concise, readable code in a lot of instances where a > statement-based version would be clumsy and noisy, and there's already some > precedent for it: > > function declaration => lambda > for-loops => ge

Re: [Python-ideas] With expressions

2018-08-03 Thread Abe Dillon
I like this idea in theory, but I'm not sold yet. I think there's a lot of draw to the concept of "expressionizing" statements because many statements require an unnatural ordering in-which the most important code, the logic, comes after some necessary but ultimately noisy (from the readers perspe

Re: [Python-ideas] With expressions

2018-08-03 Thread Todd
On Thu, Aug 2, 2018 at 5:35 AM, Ken Hilton wrote: > Hi, I don't know if someone has already suggested this before, but here > goes: > > With expressions allow using the enter/exit semantics of the with > statement inside an expression context. Examples: > > contents = f.read() with open('file

Re: [Python-ideas] With expressions

2018-08-03 Thread Chris Barker via Python-ideas
On Fri, Aug 3, 2018 at 3:49 AM, Robert Vanden Eynde wrote: > > When I say "functional programming", I speak about the paradigm used in > language like Haskell. In language like those, all constructs are > "expression-based". > sure -- but Python is explicitly NOT a functional language in that se

Re: [Python-ideas] With expressions

2018-08-03 Thread Robert Vanden Eynde
Thanks for answering each line. If someone wants "too long didn't read", just check my code at the paragraph "readlines is a toy example, but maybe the code would be more creative". Le ven. 3 août 2018 à 03:07, Steven D'Aprano a écrit : > On Thu, Aug 02, 2018 at 03:13:25PM +0200, Robert Vanden E

Re: [Python-ideas] With expressions

2018-08-02 Thread Steven D'Aprano
On Thu, Aug 02, 2018 at 03:13:25PM +0200, Robert Vanden Eynde wrote: > This brings the discussion of variable assignement in Expression. Functional > programming community seems to be more interested in python. I'm not sure what you mean there. Your English grammar is just slightly off, enough t

Re: [Python-ideas] With expressions

2018-08-02 Thread Terry Reedy
On 8/2/2018 7:53 AM, Thomas Nyberg via Python-ideas wrote: 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 wh

Re: [Python-ideas] With expressions

2018-08-02 Thread Robert Vanden Eynde
This brings the discussion of variable assignement in Expression. Functional programming community seems to be more interested in python. lines = (f.readlines() with open('hello') as f) digit = (int('hello') except ValueError: 5) value = (x+y**2 where x,y = (2,4)) values = [x+y**2 for x in range(5

Re: [Python-ideas] With expressions

2018-08-02 Thread Cody Piersall
On Thu, Aug 2, 2018 at 5:24 AM Thomas Nyberg via Python-ideas wrote: > > Is it true that Path('file').read_text() closes the file after the read? A quick look at the source confirms that the file is closed: https://github.com/python/cpython/blob/master/Lib/pathlib.py#L1174 The docstring is bette

Re: [Python-ideas] With expressions

2018-08-02 Thread Steven D'Aprano
On Thu, Aug 02, 2018 at 11:35:11AM +0200, Ken Hilton wrote: > Where this would benefit: I think the major use case is `f.read() with > open('file') as f`. [...] > Therefore `f.read() with open('file') as f`, I think, would be much > welcomed as the best way to read a file in an expression. Perhap

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

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

Re: [Python-ideas] With expressions

2018-08-02 Thread Paul Moore
On Thu, 2 Aug 2018 at 11:25, Thomas Nyberg via Python-ideas wrote: > > 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

Re: [Python-ideas] With expressions

2018-08-02 Thread Andre Roberge
On Thu, Aug 2, 2018 at 7:24 AM Thomas Nyberg via Python-ideas < python-ideas@python.org> wrote: > Is it true that Path('file').read_text() cl > oses 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

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, ma

Re: [Python-ideas] With expressions

2018-08-02 Thread Paul Moore
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

[Python-ideas] With expressions

2018-08-02 Thread Ken Hilton
Hi, I don't know if someone has already suggested this before, but here goes: 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() w