[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 4:24 PM Christopher Barker wrote: > > On Sun, Oct 24, 2021 at 9:58 AM Chris Angelico wrote: >> >> > > def puzzle(*, a=>b+1, b=>a+1): >> > > return a, b > > >> >> > > >> > > Aside: In a functional programming language >> > > a = b + 1 >> > > b = a + 1

[Python-ideas] Re: def variable = value

2021-10-24 Thread Stephen J. Turnbull
Serhiy Storchaka writes: > They have. Both function and type classes have constructors Ah, right. "First-class values", of course they do. Thanks! Steve ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Christopher Barker
On Sun, Oct 24, 2021 at 9:58 AM Chris Angelico wrote: > > > def puzzle(*, a=>b+1, b=>a+1): > > > return a, b > > > > > > Aside: In a functional programming language > > > a = b + 1 > > > b = a + 1 > > > would be a syntax (or at least compile time) error. > but it's a

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 3:12 PM Sebastian Berg wrote: > > On Mon, 2021-10-25 at 03:47 +1100, Chris Angelico wrote: > > On Mon, Oct 25, 2021 at 3:43 AM Jonathan Fine > > wrote: > > > > > > Hi > > > > > > Please forgive me if it's not already been considered. Is the > > > following valid syntax,

[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

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Jonathan Fine
Hi Chris You wrote: In fact, on subsequent consideration, I'm inclining more strongly > towards SyntaxError, due to the difficulty of explaining the actual > semantics. Changing the PEP accordingly. Your PEP, so your choice. I now think that if implemented, your PEP adds to the Python compiler

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Ben Rudiak-Gould
On Sat, Oct 23, 2021 at 5:16 PM Chris Angelico wrote: > # Very common: Use None and replace it in the function > def bisect_right(a, x, lo=0, hi=None, *, key=None): > if hi is None: > hi = len(a) > Note that if this is changed to def bisect_right(a, x, lo=0,

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Erik Demaine
On Sun, 24 Oct 2021, Erik Demaine wrote: I think the semantics are easy to specify: the argument defaults get evaluated for unspecified order, in left to right order as specified in the def. Those may trigger exceptions as usual. Sorry, that should be: I think the semantics are easy to

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 4:29 AM Erik Demaine wrote: > > On Sun, 24 Oct 2021, Erik Demaine wrote: > > > I think the semantics are easy to specify: the argument defaults get > > evaluated for unspecified order, in left to right order as specified in the > > def. Those may trigger exceptions as

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Erik Demaine
On Mon, 25 Oct 2021, Chris Angelico wrote: On Mon, Oct 25, 2021 at 3:47 AM Chris Angelico wrote: On Mon, Oct 25, 2021 at 3:43 AM Jonathan Fine wrote: Please forgive me if it's not already been considered. Is the following valid syntax, and if so what's the semantics? Here it is: def

[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: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Sebastian Berg
On Mon, 2021-10-25 at 03:47 +1100, Chris Angelico wrote: > On Mon, Oct 25, 2021 at 3:43 AM Jonathan Fine > wrote: > > > > Hi > > > > Please forgive me if it's not already been considered. Is the > > following valid syntax, and if so what's the semantics? Here it is: > > > >     def puzzle(*,

[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

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Erik Demaine
On Sun, 24 Oct 2021, Chris Angelico wrote: Is anyone interested in coauthoring this with me? Anyone who has strong interest in seeing this happen - whether you've been around the Python lists for years, or you're new and interested in getting involved for the first time, or anywhere in between!

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 3:47 AM Chris Angelico wrote: > > On Mon, Oct 25, 2021 at 3:43 AM Jonathan Fine wrote: > > > > Hi > > > > Please forgive me if it's not already been considered. Is the following > > valid syntax, and if so what's the semantics? Here it is: > > > > def puzzle(*,

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 3:43 AM Jonathan Fine wrote: > > Hi > > Please forgive me if it's not already been considered. Is the following valid > syntax, and if so what's the semantics? Here it is: > > def puzzle(*, a=>b+1, b=>a+1): > return a, b > > Aside: In a functional programming

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Jonathan Fine
Hi Please forgive me if it's not already been considered. Is the following valid syntax, and if so what's the semantics? Here it is: def puzzle(*, a=>b+1, b=>a+1): return a, b Aside: In a functional programming language a = b + 1 b = a + 1 would be a syntax (or at least

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread David Mertz, Ph.D.
Yes! Exactly that. I believe (and believed in some discussions here since maybe 4-5 years ago) that having something close to the dask.delayed() function baked into the language works be a good think. And as a narrow point, it could address the narrower late-bound function argument matter as one

[Python-ideas] Re: Deferred evaluation (was: PEP 671: Syntax for late-bound function argument defaults)

2021-10-24 Thread David Mertz, Ph.D.
On Sun, Oct 24, 2021, 10:11 AM Chris Angelico > Not sure I understand. Your example was something like: > > def fn2(thing): > a, b = 13, 21 > x = 5 > print("Thing is:", thing) > > def f(x=defer: a + b): > a, b = 3, 5 > fn2(defer: x) > return x > > So inside f(), "defer: a

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 1:16 AM Zomatree . wrote: > > What would the the syntax for the default be, i would presume just a single > expression like a lambda, but what about something like an async function, > for example: > ```py > async def foo(arg=>await get_arg()): > ``` > would this work?

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Zomatree .
What would the the syntax for the default be, i would presume just a single expression like a lambda, but what about something like an async function, for example: ```py async def foo(arg=>await get_arg()): ``` would this work? ___ Python-ideas mailing

[Python-ideas] Deferred evaluation (was: PEP 671: Syntax for late-bound function argument defaults)

2021-10-24 Thread Chris Angelico
On Mon, Oct 25, 2021 at 12:47 AM David Mertz, Ph.D. wrote: > > On Sun, Oct 24, 2021, 12:20 AM Chris Angelico >> >> How would it know to look for a and b inside fn2's scope, instead of looking >> for x inside fn2's scope? > > > The same way 'eval("a+b")' knows to look in the local scope when

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 09:39:27AM -0400, David Mertz, Ph.D. wrote: > This has been a topic of other threads over the years, and something I've > wanted at least since I first worked with Dask's 'delayed()' function. You mean this? https://docs.dask.org/en/latest/delayed.html -- Steve

[Python-ideas] Re: def variable = value

2021-10-24 Thread Jonathan Fine
Hi Serhiy Thank you for so clearly explaining how names get passed to function and class constructors. You also wrote: > We do not have generalized way to call arbitrary constructor with > automatically passing __name__, __qualname__ and __module__. And it would > be convenient. > > create

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Steven D'Aprano
On Sun, Oct 24, 2021 at 12:39:38PM +0100, Matt del Valle wrote: > I'll confess though I'm not a fan of any of the alternate syntaxes. I think > => works really well, particularly if lambdas in the form: `(*args) => > expr` are added at some point in the future So if we have the arrow shortcut

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread David Mertz, Ph.D.
On Sun, Oct 24, 2021, 12:20 AM Chris Angelico > How would it know to look for a and b inside fn2's scope, instead of > looking for x inside fn2's scope? > The same way 'eval("a+b")' knows to look in the local scope when evaluated. I mean, of course 'x' could be rebound in some scope before it

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread David Mertz, Ph.D.
On Sun, Oct 24, 2021, 12:25 AM Guido van Rossum > I am worried that this side-thread about dynamic scopes (which are a > ridiculous idea IMO) will derail the decent proposal of the PEP. > It's really not a suggestion about dynamic scoping but about more generalized deferred computation. This

[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

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

[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

[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

[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 >

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

[Python-ideas] Re: def variable = value

2021-10-24 Thread Serhiy Storchaka
24.10.21 15:20, Stephen J. Turnbull пише: > What do you mean by "constructor" here? Normally that word refers to > methods that populate the attributes of instances (in Python, __init__ > and__new__). But functions and methods don't have such, so you must > mean something else? They have. Both

[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

[Python-ideas] Re: def variable = value

2021-10-24 Thread Stephen J. Turnbull
Jonathan Fine writes: > >From my phone. > > An important thing about def x and class A is that the strings x and A are > made available to the constructor for x and A respectively. What do you mean by "constructor" here? Normally that word refers to methods that populate the attributes of

[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: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Matt del Valle
Agreed on the point about PEP 661, if this is accepted I don't think it will have much to offer. For what it's worth I'm a very strong +1 on PEP 671 as it stands. Thanks for showing me something I didn't even know I wanted Chris :) I'll confess though I'm not a fan of any of the alternate

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Chris Angelico
On Sun, Oct 24, 2021 at 9:34 PM Paul Moore wrote: > > This should probably reference PEP 661 (Sentinel Values) which is > being discussed on Discourse: > https://discuss.python.org/t/pep-661-sentinel-values/9126 > > It's a different proposal, but one of the major motivating use cases > (if not

[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

[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

[Python-ideas] Re: PEP 671: Syntax for late-bound function argument defaults

2021-10-24 Thread Paul Moore
This should probably reference PEP 661 (Sentinel Values) which is being discussed on Discourse: https://discuss.python.org/t/pep-661-sentinel-values/9126 It's a different proposal, but one of the major motivating use cases (if not the only one) for sentinels is handling function default values

[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

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

2021-10-24 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-24 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

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

2021-10-24 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