Re: [Python-ideas] Discussion: Duck typing with “concepts”

2019-01-22 Thread Marko Ristin-Kaufmann
Hi James, As Ivan has mentioned, Protocols already allow for statical type checks: https://mypy.readthedocs.io/en/latest/protocols.html We didn't need protocols that often at Parquery, maybe half a dozen of times? While we didn't use them in Python, we had to use them intensively in Go where it

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread Steven D'Aprano
On Mon, Jan 21, 2019 at 05:56:17PM +1100, Steven D'Aprano wrote: [...] > > Variable names that are declared but have not been assigned to will be > > considered to exist for the purposes of the backtick expression. > > Python doesn't have variable declarations, so I don't know what this >

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Steven D'Aprano
I've been thinking more about this proposal, and realised why I've been feeling a slight sense of disquiet about it. I think it encourages an anti-pattern of catching too much. (Or at least a code smell.) Although we're all guilty of violating this principle from time to time, in general we

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Steven D'Aprano
On Tue, Jan 22, 2019 at 03:22:27PM -0700, Paul Ferrell wrote: > Anecdotally, I showed the with/except example to my student (who's > relatively new to python), to see how he interpreted it. He (correctly?) > assumed the CM operations were within the 'try', and was pretty surprised > when I told

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread Stephen J. Turnbull
Jonathan Fine writes: > > Backtick expressions work exactly like lambdas, except that they > > are bound to the instance they are created in every time that > > class is used to create one. > > I would if possible very much like to see some real world examples of > Python code, that would

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Paul Ferrell
I completely understand your perspective, and agree with most of it. It doesn't add new expressiveness, it adds a bit of polish (and I think completeness) to the relatively new concept of 'with' statements. Is this so intuitive that we don't actually have to teach it? Is it such a natural

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Paul Ferrell
That is definitely an ambiguity worth considering (whether __enter__ is within the implied 'try'). Anecdotally, I showed the with/except example to my student (who's relatively new to python), to see how he interpreted it. He (correctly?) assumed the CM operations were within the 'try', and was

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Steven D'Aprano
On Tue, Jan 22, 2019 at 01:11:10PM -0700, Paul Ferrell wrote: [...] > I would like to propose that the syntax for 'with' blocks > be changed such that they can be accompanied by 'except', 'finally', > and/or 'else' blocks as per a standard 'try' block. What benefit does this give apart from

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Nathaniel Smith
The first concern that comes to my mind is... When I see: with: ... except: ... Is that a shorthand for try: with: ... except: ... or for with: try: ... except: ... ? Both are plausible, and it makes a big difference, because 'with' already has

[Python-ideas] Fwd: Potential PEP: with/except

2019-01-22 Thread Paul Ferrell
> > The thing that concerns me is that any such problem and solution seems > to apply equally to any other kind of block. Why not allow excepts on fo > loops, for example? > Very good point. I think 'with' is special in that it typically contains the entirety of the use of an object, and the

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread Christopher Barker
Going back to the original post: On Sun, Jan 20, 2019 at 6:43 PM James Lu wrote: > Backtick expressions work exactly like lambdas, except that they are bound > to the instance they are created in every time that class is used to create > one. ?!? bound every time that instance is used to

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Barry Scott
> On 22 Jan 2019, at 20:11, Paul Ferrell wrote: > > I've found that almost any time I'm writing a 'with' block, it's doing > something that could throw an exception. As a result, each of those > 'with' blocks needs to be nested within a 'try' block. Due to the > nature of 'with', it is rarely

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Chris Angelico
On Wed, Jan 23, 2019 at 7:11 AM Paul Ferrell wrote: > As a result, I would like to propose that the syntax for 'with' blocks > be changed such that they can be accompanied by 'except', 'finally', > and/or 'else' blocks as per a standard 'try' block. These would handle > exceptions that occur in

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread David Mertz
You could write a context manager that used an arbitrary callback passed in to handle exceptions (including re-raising as needed). This doesn't require new syntax, just writing a custom CM. On Tue, Jan 22, 2019, 4:20 PM Barry Scott > > On 22 Jan 2019, at 20:31, Michael Selik wrote: > > On Tue,

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Barry Scott
> On 22 Jan 2019, at 20:31, Michael Selik wrote: > > On Tue, Jan 22, 2019, 12:11 PM Paul Ferrell wrote: > I see this as the natural evolution of what 'with' is all about - replacing > necessary try-finally blocks with something more elegant. We just didn't >

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread Barry Scott
The problem with using the back-tick is that it is far too easy to miss read it for a single-quote. back-tick in bash has the $( xxx ) replacement that avoids the problem. Please find an alternative syntax that avoid the problem. Barry > On 22 Jan 2019, at 13:42, James Lu wrote: > > Later

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Michael Selik
On Tue, Jan 22, 2019, 12:11 PM Paul Ferrell I see this as the natural evolution of what 'with' is all about - > replacing necessary try-finally blocks with something more elegant. We just > didn't include the 'except' portion. > The time machine strikes again. In fact, you can handle exceptions

Re: [Python-ideas] Potential PEP: with/except

2019-01-22 Thread Calvin Spealman
On Tue, Jan 22, 2019 at 3:11 PM Paul Ferrell wrote: > I've found that almost any time I'm writing a 'with' block, it's doing > something that could throw an exception. As a result, each of those > 'with' blocks needs to be nested within a 'try' block. Due to the > nature of 'with', it is rarely

[Python-ideas] Potential PEP: with/except

2019-01-22 Thread Paul Ferrell
I've found that almost any time I'm writing a 'with' block, it's doing something that could throw an exception. As a result, each of those 'with' blocks needs to be nested within a 'try' block. Due to the nature of 'with', it is rarely (if ever) the case that the try block contains anything other

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread Stephan Hoyer
On Mon, Jan 21, 2019 at 8:47 AM Jonathan Fine wrote: > > Backtick expressions work exactly like lambdas, except that they are > bound to the instance they are created in every time that class is used to > create one. > > I would if possible very much like to see some real world examples of >

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread James Lu
I’m a little busy recently, so I’ll reply to as much as I can now and reply to the rest later. Scratch the stuff I said about scope. Backtick expressions should inherit the scope normally like any other nested function. > That's different behaviour from regular functions, where names are only

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread James Lu
Later today I will send a working implementation of backtick expressions as a function call. ___ Python-ideas mailing list Python-ideas@python.org https://mail.python.org/mailman/listinfo/python-ideas Code of Conduct:

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread James Lu
> On Jan 21, 2019, at 1:56 AM, Steven D'Aprano wrote: > > It disturbs me that you believe you get to tell everyone what syntax > highlighting they should use for this feature. That's pretty > dictatorial, and not in a good BDFL way. I don’t want to tell anyone how to make their syntax

Re: [Python-ideas] Backtick expression: similar to a shorter lambda syntax

2019-01-22 Thread James Lu
> On Jan 21, 2019, at 1:56 AM, Steven D'Aprano wrote: > > It disturbs me that you believe you get to tell everyone what syntax > highlighting they should use for this feature. That's pretty > dictatorial, and not in a good BDFL way. I don’t want to tell anyone how to make their syntax

Re: [Python-ideas] Discussion: Duck typing with “concepts”

2019-01-22 Thread Ivan Levkivskyi
I think you may be a bit late. Have you heard about PEP 544? -- Ivan On Tue, 22 Jan 2019 at 11:50, James Lu wrote: > So here’s an interesting idea, not a proposal yet. > > In C++20, a Concept is a list of Boolean expressions with a name that can > be used in place of a type in a templated

[Python-ideas] Discussion: Duck typing with “concepts”

2019-01-22 Thread James Lu
So here’s an interesting idea, not a proposal yet. In C++20, a Concept is a list of Boolean expressions with a name that can be used in place of a type in a templated (ie type-generic) function. from typing import Concept Iterator = Concept(lambda o: hasattr(o, "__iter__", lambda o: iter(o) !=