On Mon, 26 Dec 2022 at 04:53, Steven D'Aprano <st...@pearwood.info> wrote: > > On Sat, Dec 24, 2022 at 11:34:19AM -0500, Shironeko wrote: > > > > Is the => syntax needed? as far as I can think of, the only time where > > late evaluation is needed is when the expression references the other > > arguments. > > You are missing the most common case, the motivating case, for > late-bound defaults: mutable defaults. > > def spam(x, y=>[]): > pass
Exactly - this is the most important reason. So the true reason for the => syntax is: in order to gain late-bound defaults, we have to distinguish them from early-bound defaults (because changing ALL function default arguments to late-bound would be a massive breaking change). There's no particular reason for it to be "=>" specifically, and other syntax options have been considered, but it does need to be something other than "=". > The ability for default values to refer to other parameters is a Nice To > Have, not a Must Have. It has been a very long time since I have read > the PEP, and I don't remember whether it reviews other languages to see > what functionality they provide for defaults, but I don't think many > other languages allow you to set the default of one parameter to be > another parameter. JavaScript does, by nature of its extremely simplistic definition of argument defaults. function f(x=1, y=x) {console.log("--> x = ", x, ", y = ", y);} f(5) --> x = 5 , y = 5 f() --> x = 1 , y = 1 f(42, undefined) --> x = 42 , y = 42 f(undefined, 123) --> x = 1 , y = 123 Ruby does: $ irb irb(main):001:1* def f(x=1, y=x) irb(main):002:1* puts "x = #{x}, y = #{y}" irb(main):003:0> end => :f irb(main):004:0> f() x = 1, y = 1 => nil irb(main):005:0> f(123) x = 123, y = 123 => nil I suspect that, in each case, the rule is quite simple: the argument default is evaluated in the context of the function's body. Exactly the same as PEP 671 proposes. Any other languages to test? ChrisA _______________________________________________ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an email to python-ideas-le...@python.org https://mail.python.org/mailman3/lists/python-ideas.python.org/ Message archived at https://mail.python.org/archives/list/python-ideas@python.org/message/CIPBJHCLGEUEVLOHPEA6WYNNCD63A7NP/ Code of Conduct: http://python.org/psf/codeofconduct/