[Python-ideas] Re: Syntax for late-bound arguments

2021-10-28 Thread Rob Cliffe via Python-ideas
+1 on the idea. Sometimes early binding is needed, sometimes late binding is needed.  So Python should provide both.  QED 😁 I'm not keen on the     var = > expr syntax.  IMO the arrow is pointing the wrong way.  expr is assigned to var. Some possible alternatives, if there is no technical reaso

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-25 Thread Ethan Furman
On 10/23/21 8:04 PM, Bruce Leban wrote: > I was talking about (2) but I should have been explicit. And yes, you highlight a potential source of confusion. > > def f(x=>x + 1): ... > > means that x is 1 more than the value of x from the enclosing global scope (at function call time) while Uh, n

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 05:24:46PM +0400, Abdulla Al Kathiri wrote: > It’s not a good idea to use a mutable object anyways a default value. Unless you intend to use a mutable object as a default value, and have it persist from one call to the next. Then it is absolutely fine. One of the use-cas

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Christopher Barker
+1 to this idea -- thanks Chris A! On Sat, Oct 23, 2021 at 2:01 PM Bruce Leban wrote: > Here's one way you could imagine writing this today: >> > > def bisect(a, x, lo=0, hi=lambda: len(a)): > hi = hi() if callable(hi) else hi > ... > > which is clumsy and more importantly do

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Rob Cliffe via Python-ideas
On 24/10/2021 03:48, Steven D'Aprano wrote: On Sun, Oct 24, 2021 at 01:16:02PM +1100, Chris Angelico wrote: And is it really a problem if we delay the late-binding to the point where the value is actually needed? Here's a toy example: # Using only early binding. def function(spam,

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Guido van Rossum
More about thunks. I've resisted the temptation to look at the Wikipedia page -- this is from memory. On Sat, Oct 23, 2021 at 11:39 PM Steven D'Aprano wrote: > > I would prefer to see this situation handled as part of a > > larger-scale > > change of adding some kind of "inline lambd

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Abdulla Al Kathiri
Yeah I realized that when I went to my laptop hhh. Hence the previous email correcting it. > On 24 Oct 2021, at 5:19 PM, Steven D'Aprano wrote: > > On Sun, Oct 24, 2021 at 04:57:16PM +0400, Abdulla Al Kathiri wrote: > >> def insort_right(a, x, lo=0, hi={len(a)}, *, key=None): … > > That's a

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Abdulla Al Kathiri
Oops I forgot that this could evaluate to a set if “a” was defined beforehand. Maybe not a good idea after all. It’s not a good idea to use a mutable object anyways a default value. > On 24 Oct 2021, at 4:57 PM, Abdulla Al Kathiri > wrote: > > How about this syntax: > > def insort_right(a, x

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 04:57:16PM +0400, Abdulla Al Kathiri wrote: > def insort_right(a, x, lo=0, hi={len(a)}, *, key=None): … That's already legal syntax for a default value of a set with one element, len(a). -- Steve ___ Python-ideas mailing lis

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 10:18:52PM +1100, Steven D'Aprano wrote: > Find some languages (Lisp, Smalltalk, any others?) and show that they > are abused by people in that community. Oops, sorry I left out a clause. Find some language **with late-binding defaults** and show that they are abused by

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Abdulla Al Kathiri
How about this syntax: def insort_right(a, x, lo=0, hi={len(a)}, *, key=None): … Similar to the expression curly brackets in f-string. If the user didn’t specify a value for hi, the expression between the curly brackets will be evaluated and assigned to hi. Abdulla Sent from my iPhone > O

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Chris Angelico
On Sun, Oct 24, 2021 at 11:45 PM Ricky Teachey wrote: > > It seems to me that the syntax for late binding could be chosen so as to > leave the possibility of expansion open in the future, and defer (har har) > the entire generalized thunk discussion? > > So why not go with syntax like this, wher

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Ricky Teachey
It seems to me that the syntax for late binding could be chosen so as to leave the possibility of expansion open in the future, and defer (har har) the entire generalized thunk discussion? So why not go with syntax like this, where before the ? just represents a keyword to be bike shedded ("defer"

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Chris Angelico
On Sun, Oct 24, 2021 at 9:52 PM Serhiy Storchaka wrote: > > 23.10.21 19:07, Chris Angelico пише: > > _missing = object() > > def spaminate(thing, count=_missing): > > if count is _missing: count = thing.getdefault() > > > > Proposal: Proper syntax and support for late-bound argument defaults.

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Steven D'Aprano
On Sat, Oct 23, 2021 at 11:47:59PM -0700, Brendan Barnwell wrote: > It's true that any feature can be abused, but I think this > temptation to shoehorn function logic into the argument list will be > more > likely to result in unwieldy signatures than the current situation. That sh

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Serhiy Storchaka
23.10.21 19:07, Chris Angelico пише: > _missing = object() > def spaminate(thing, count=_missing): > if count is _missing: count = thing.getdefault() > > Proposal: Proper syntax and support for late-bound argument defaults. > > def spaminate(thing, count=:thing.getdefault()): > ... Few y

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Brendan Barnwell
On 2021-10-23 23:33, Steven D'Aprano wrote: On Sat, Oct 23, 2021 at 08:29:54PM -0700, Brendan Barnwell wrote: For me the biggest problem with this idea is that it only handles a subset of cases, namely those that can be expressed as an expression inlined into the function definition.

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Ben Rudiak-Gould
On Sat, Oct 23, 2021 at 11:53 PM Steven D'Aprano wrote: > If we had thunks, that would give us late binding for free: > > def bisect(a, x, lo=0, hi=thunk len(a), *, key=None) > I'm unclear on exactly what the semantics of a thunk would be, but I don't see how it could do what you want here.

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-24 Thread Chris Angelico
On Sun, Oct 24, 2021 at 5:53 PM Steven D'Aprano wrote: > > On Sun, Oct 24, 2021 at 02:09:59PM +1100, Chris Angelico wrote: > > > The biggest problem with thunks is knowing when to trigger evaluation. > > I think Algol solved that problem by having thunks a purely internal > mechanism, not a first-

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 02:09:59PM +1100, Chris Angelico wrote: > The biggest problem with thunks is knowing when to trigger evaluation. I think Algol solved that problem by having thunks a purely internal mechanism, not a first-class value that users could store or pass around. > We already

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Steven D'Aprano
On Sat, Oct 23, 2021 at 08:29:54PM -0700, Brendan Barnwell wrote: > For me the biggest problem with this idea is that it only handles a > subset of cases, namely those that can be expressed as an expression > inlined into the function definition. True. But that's equally true for default va

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 3:51 PM Brendan Barnwell wrote: > > On 2021-10-23 09:07, Chris Angelico wrote: > > Proposal: Proper syntax and support for late-bound argument defaults. > > > > def spaminate(thing, count=:thing.getdefault()): > > ... > > I'm -1 on it. > > For me the bi

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Brendan Barnwell
On 2021-10-23 09:07, Chris Angelico wrote: Proposal: Proper syntax and support for late-bound argument defaults. def spaminate(thing, count=:thing.getdefault()): ... I'm -1 on it. For me the biggest problem with this idea is that it only handles a subset of cases, namely

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread 2QdxY4RzWzUUiLuE
On 2021-10-24 at 13:23:51 +1100, Steven D'Aprano wrote: > On Sat, Oct 23, 2021 at 02:54:54PM -0700, 2qdxy4rzwzuui...@potatochowder.com > wrote: > > [...] > > > The function header is a syntactic construct - the "def" line, any > > > decorators, annotations, etc. > > > > If you mean that def st

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 1:53 PM Steven D'Aprano wrote: > > On Sun, Oct 24, 2021 at 01:16:02PM +1100, Chris Angelico wrote: > > > What I'm more often seeing is cases that are less obviously a > > late-binding, but where the sentinel is replaced with the "real" value > > at the point where it's used

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Bruce Leban
On Sat, Oct 23, 2021 at 7:55 PM Steven D'Aprano wrote: > > And is it really a problem if we delay the late-binding to the point > where the value is actually needed? ... [in that csse] I would stick to manual late- > binding using None, and only evaluate it as needed. > > Maybe this i

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Bruce Leban
--- Bruce On Sat, Oct 23, 2021 at 7:55 PM Chris Angelico wrote: > On Sun, Oct 24, 2021 at 1:48 PM Bruce Leban wrote: > > > > > > On Sat, Oct 23, 2021 at 6:23 PM Jelle Zijlstra > wrote: > >> > >> In the PEP's example: > >> > >> def bisect_right(a, x, lo=0, hi=>len(a), *, key=None): > >> > >>

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Bruce Leban
On Sat, Oct 23, 2021 at 7:00 PM Steven D'Aprano wrote: > I challenge that assertion. I've never knowingly seen a function where > the late binding is "buried deeper in the function", certainly not deep > enough that it is not obvious. It is a very strong convention that such > late binding operat

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 01:16:02PM +1100, Chris Angelico wrote: > What I'm more often seeing is cases that are less obviously a > late-binding, but where the sentinel is replaced with the "real" value > at the point where it's used, rather than up the top of the function. Got any examples you can

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 1:48 PM Bruce Leban wrote: > > > On Sat, Oct 23, 2021 at 6:23 PM Jelle Zijlstra > wrote: >> >> In the PEP's example: >> >> def bisect_right(a, x, lo=0, hi=>len(a), *, key=None): >> >> This reads to me like we're putting "hi" into "len(a)", when it's in fact >> the revers

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Bruce Leban
On Sat, Oct 23, 2021 at 6:23 PM Jelle Zijlstra wrote: > In the PEP's example: > > def bisect_right(a, x, lo=0, hi=>len(a), *, key=None): > > This reads to me like we're putting "hi" into "len(a)", when it's in fact > the reverse. > I think in most cases what's on the right side will be something

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Steven D'Aprano
On Sat, Oct 23, 2021 at 02:54:54PM -0700, 2qdxy4rzwzuui...@potatochowder.com wrote: [...] > > The function header is a syntactic construct - the "def" line, any > > decorators, annotations, etc. > > If you mean that def statements and decorators run at compile time, then > I agree. If you mean

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 1:12 PM Ricky Teachey wrote: > > This might be a dumb question but is the context built only left to right? Or > will this be legal? > > def f(a, *, b=:c, c=:b): > print(f"{a=} {b=} {c=}") > > >>> f(1, b=2) > a=1 b=2 c=2 > > >>> f(1, c=2) > a=1 b=2 c=2 > That's someth

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 1:00 PM Steven D'Aprano wrote: > > On Sun, Oct 24, 2021 at 06:54:36AM +1100, Chris Angelico wrote: > > [...] > > Teaching moment. Currently, the equivalent second function would be this: > > > > def f2(l=None): > > if l is None: l = [] > > l.append(4) > > return

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Ricky Teachey
This might be a dumb question but is the context built only left to right? Or will this be legal? def f(a, *, b=:c, c=:b): print(f"{a=} {b=} {c=}") >>> f(1, b=2) a=1 b=2 c=2 >>> f(1, c=2) a=1 b=2 c=2 ___ Python-ideas mailing list -- python-ideas@py

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 12:44 PM Rob Cliffe wrote: > I'm not keen on the > var = > expr > syntax. IMO the arrow is pointing the wrong way. expr is assigned to var. That's definitely open to discussion. > Some possible alternatives, if there is no technical reason they > wouldn't work (as

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 06:54:36AM +1100, Chris Angelico wrote: [...] > Teaching moment. Currently, the equivalent second function would be this: > > def f2(l=None): > if l is None: l = [] > l.append(4) > return l > > And the whole "early bind or late bind" question is there just the

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Jelle Zijlstra
El sáb, 23 oct 2021 a las 12:57, Guido van Rossum () escribió: > I like that you're trying to fix this wart! I think that using a different > syntax may be the only way out. My own bikeshed color to try would be `=>`, > assuming we'll introduce `(x) => x+1` as the new lambda syntax, but I can > se

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 9:05 AM Ethan Furman wrote: > > On 10/23/21 2:23 PM, Chris Angelico wrote: > > > It seems like there's broad interest in this, but a lot of details to > > nut out. I think it may be time for me to write up a full PEP. Guido, > > if I'm understanding recent SC decisions c

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 8:56 AM <2qdxy4rzwzuui...@potatochowder.com> wrote: > > On 2021-10-24 at 06:54:36 +1100, > Chris Angelico wrote: > > > On Sun, Oct 24, 2021 at 6:18 AM <2qdxy4rzwzuui...@potatochowder.com> wrote: > > > > The expression would be evaluated in the function's context, having > >

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Ethan Furman
On 10/23/21 2:23 PM, Chris Angelico wrote: > It seems like there's broad interest in this, but a lot of details to > nut out. I think it may be time for me to write up a full PEP. Guido, > if I'm understanding recent SC decisions correctly, a PEP editor can > self-sponsor, correct? Of all people

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread 2QdxY4RzWzUUiLuE
On 2021-10-24 at 06:54:36 +1100, Chris Angelico wrote: > On Sun, Oct 24, 2021 at 6:18 AM <2qdxy4rzwzuui...@potatochowder.com> wrote: > > > The expression would be evaluated in the function's context, having > > > available to it everything that the function has. Notably, this is NOT > > > the sam

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 7:58 AM Bruce Leban wrote: > > On Sat, Oct 23, 2021 at 12:56 PM Guido van Rossum wrote: >> >> I like that you're trying to fix this wart! I think that using a different >> syntax may be the only way out. My own bikeshed color to try would be `=>`, >> assuming we'll intro

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 6:55 AM Guido van Rossum wrote: > > I like that you're trying to fix this wart! I think that using a different > syntax may be the only way out. My own bikeshed color to try would be `=>`, > assuming we'll introduce `(x) => x+1` as the new lambda syntax, but I can see >

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Bruce Leban
On Sat, Oct 23, 2021 at 12:56 PM Guido van Rossum wrote: > I like that you're trying to fix this wart! I think that using a different > syntax may be the only way out. My own bikeshed color to try would be `=>`, > assuming we'll introduce `(x) => x+1` as the new lambda syntax, but I can > see pro

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Paul Moore
On Sat, 23 Oct 2021 at 17:09, Chris Angelico wrote: > > Proposal: Proper syntax and support for late-bound argument defaults. > > def spaminate(thing, count=:thing.getdefault()): > ... > > def bisect(a, x, lo=0, hi=:len(a)): > if lo < 0: > raise ValueError('lo must be non-negative'

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 6:18 AM <2qdxy4rzwzuui...@potatochowder.com> wrote: > > The expression would be evaluated in the function's context, having > > available to it everything that the function has. Notably, this is NOT > > the same as the context of the function definition, but this is only > >

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Guido van Rossum
I like that you're trying to fix this wart! I think that using a different syntax may be the only way out. My own bikeshed color to try would be `=>`, assuming we'll introduce `(x) => x+1` as the new lambda syntax, but I can see problems with both as well :-). -- --Guido van Rossum (python.org/~g

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread 2QdxY4RzWzUUiLuE
On 2021-10-24 at 03:07:45 +1100, Chris Angelico wrote: > def bisect(a, x, lo=0, hi=None): > if lo < 0: > raise ValueError('lo must be non-negative') > if hi is None: > hi = len(a) > > It's clear what value lo gets if you omit it. It's less clear what hi > gets. And the si

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 4:12 AM Ethan Furman wrote: > > On 10/23/21 9:44 AM, Chris Angelico wrote: > > > Feel free to propose an improvement to the syntax. Whatever spelling > > is ultimately used, this would still be of value. > > def bisect(a, x, lo=0, hi?=len(a)): > Ah, okay. I had cons

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Ethan Furman
On 10/23/21 9:44 AM, Chris Angelico wrote: > Feel free to propose an improvement to the syntax. Whatever spelling > is ultimately used, this would still be of value. def bisect(a, x, lo=0, hi?=len(a)): -- ~Ethan~ ___ Python-ideas mailing list -- p

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Chris Angelico
On Sun, Oct 24, 2021 at 3:26 AM Ethan Furman wrote: > > On 10/23/21 9:07 AM, Chris Angelico wrote: > > > Proposal: Proper syntax and support for late-bound argument defaults. > > I like the proposal. > > > def spaminate(thing, count=:thing.getdefault()): > > ... > > > > def bisect(a, x,

[Python-ideas] Re: Syntax for late-bound arguments

2021-10-23 Thread Ethan Furman
On 10/23/21 9:07 AM, Chris Angelico wrote: > Proposal: Proper syntax and support for late-bound argument defaults. I like the proposal. > def spaminate(thing, count=:thing.getdefault()): > ... > > def bisect(a, x, lo=0, hi=:len(a)): > if lo < 0: > raise ValueError('lo must be