[Python-ideas] Re: Optional keyword arguments

2020-06-02 Thread Ricky Teachey
BTW, going back to the question of mutable defaults, it occurs to me > that there is an "obvious" idiom for self-documenting sentinels for > defaults that are deferred because you want a new instance each time > the function is called: use the constructors! Here are some empty > mutables: > >

[Python-ideas] Re: Optional keyword arguments

2020-06-02 Thread Rob Cliffe via Python-ideas
On 26/05/2020 07:09, Greg Ewing wrote: Wild idea: Instead of sentinels, have a way of declaring optional arguments with no default, and a way of conditionally assigning a value to them if they are not bound. E.g.     def order(eggs = 4, spam =):     spam ?= Spam() Here the '?=' means

[Python-ideas] Re: Optional keyword arguments

2020-05-31 Thread Stephen J. Turnbull
Greg Ewing writes: > You've probably never seen the one I'm thinking of. It's a > proprietary, vaguely VB-like language used for scripting a > particular application. It doesn't work like Lua, but it > does have scoping rules that can lead to some surprising > results. By contrast, Lisps

[Python-ideas] Re: Optional keyword arguments

2020-05-31 Thread Stephen J. Turnbull
David Mertz writes: > On Fri, May 29, 2020 at 1:12 PM Alex Hall wrote: > > > def foo(a=17, b=42,, x=delayed randint(0,9), y=delayed randrange(1,100)): > > > >> if something: > >> # The simple case is realizing a direct delayed > >> val = concretize x > >> elif

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Greg Ewing
On 31/05/20 1:31 am, Steven D'Aprano wrote: On Thu, May 28, 2020 at 10:27:47PM +1200, Greg Ewing wrote: On 28/05/20 7:31 pm, Chris Angelico wrote: Is it an implementation detail that 4 will be used for eggs if it isn't passed? The default value of eggs being 4 is a static fact, but creating

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Steven D'Aprano
On Thu, May 28, 2020 at 10:27:47PM +1200, Greg Ewing wrote: > On 28/05/20 7:31 pm, Chris Angelico wrote: > >Is it an implementation detail that 4 will be used for eggs if it > >isn't passed? > > That feels different to me somehow. I think it has something to do > with declarative vs. procedural

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Alex Hall
On Sat, May 30, 2020 at 6:00 AM Steven D'Aprano wrote: > On Fri, May 29, 2020 at 06:03:30PM +0200, Alex Hall wrote: > > > > I never said that Python's scoping rules were implementation details. > > > > > > I said that the storage mechanism of *how* local variables are stored, > > > and hence

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Steven D'Aprano
On Thu, May 28, 2020 at 10:37:58PM +1200, Greg Ewing wrote: > I think we need a real example to be able to talk about this > meaningfully. > > But I'm having trouble thinking of one. I can't remember ever > writing a function with a default argument value that *has* to > be mutable and *has* to

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Greg Ewing
On 30/05/20 8:03 pm, Steven D'Aprano wrote: Is this `?=` idea for a general purpose operator we can use anywhere we like? I introduced it as part of a two-part idea for allowing optional parameters without a default. But there would be nothing to stop you from using it elsewhere. The syntax

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Greg Ewing
On 30/05/20 7:15 pm, Steven D'Aprano wrote: Out of curiosity, which languages are you thinking of? I know Lua does that, I can't think of any others. You've probably never seen the one I'm thinking of. It's a proprietary, vaguely VB-like language used for scripting a particular application. It

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Chris Angelico
On Sat, May 30, 2020 at 6:13 PM Steven D'Aprano wrote: > > On Sat, May 30, 2020 at 04:34:49PM +1200, Greg Ewing wrote: > > On 30/05/20 12:11 am, Steven D'Aprano wrote: > > >I said that the storage mechanism of *how* local variables are stored, > > >and hence whether or not writes to `locals()`

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Steven D'Aprano
On Sat, May 30, 2020 at 04:34:49PM +1200, Greg Ewing wrote: > On 30/05/20 12:11 am, Steven D'Aprano wrote: > >I said that the storage mechanism of *how* local variables are stored, > >and hence whether or not writes to `locals()` are reflected in the > >local variables, is an implementation

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Greg Ewing
On 30/05/20 4:28 pm, Steven D'Aprano wrote: py> len() TypeError: len() takes exactly one argument (0 given) to be replaced with something like this: UnboundLocalError: local variable referenced before assignment Not in my version of the idea, because a parameter would only be

[Python-ideas] Re: Optional keyword arguments

2020-05-30 Thread Steven D'Aprano
On Sat, May 30, 2020 at 04:41:58PM +1200, Greg Ewing wrote: > On 30/05/20 2:52 am, Dominik Vilsmeier wrote: > >Indeed locals are special, but why was it designed this way? Why not > >resolve such an unbound local name in the enclosing scopes? > > From experience with other languages I can attest

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Greg Ewing
On 30/05/20 2:52 am, Dominik Vilsmeier wrote: Indeed locals are special, but why was it designed this way? Why not resolve such an unbound local name in the enclosing scopes? From experience with other languages I can attest that "sometimes local, sometimes global depending on what gets

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Steven D'Aprano
Attempting to bring things back on topic to function parameters with late binding. On Fri, May 29, 2020 at 06:03:30PM +0200, Alex Hall wrote: > Chris was just saying that a statement like `x ?= y` meaning 'assign the > value of y to the local variable x if x is currently unbound' already fits >

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Greg Ewing
On 30/05/20 12:11 am, Steven D'Aprano wrote: I said that the storage mechanism of *how* local variables are stored, and hence whether or not writes to `locals()` are reflected in the local variables, is an implementation detail. That's true, but just to be clear, my ?= idea doesn't rely on

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Greg Ewing
On 29/05/20 11:49 pm, Steven D'Aprano wrote: Where else would you put the parameter defaults, if not in the parameter list? In some part of the function that's not the parameter list. :-) There are many ways it could be done. I've suggested one already (a special assignment statement).

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Steven D'Aprano
On Fri, May 29, 2020 at 06:03:30PM +0200, Alex Hall wrote: > > I never said that Python's scoping rules were implementation details. > > > > I said that the storage mechanism of *how* local variables are stored, > > and hence whether or not writes to `locals()` are reflected in the > > local

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Steven D'Aprano
On Fri, May 29, 2020 at 04:52:38PM +0200, Dominik Vilsmeier wrote: > Indeed locals are special, but why was it designed this way? Why not > resolve such an unbound local name in the enclosing scopes? Probably for speed. When I first learned about the LGB rule (so long ago there wasn't even an

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Rhodri James
On 29/05/2020 18:02, David Mertz wrote: I think this is a perfect example of where my desired "delayed" (or "deferred") construct would be great. There are lots of behaviors that I have not thought through, and do not specify here. But for example: def foo(a=17, b=42,, x=delayed randint(0,9),

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread David Mertz
On Fri, May 29, 2020 at 1:12 PM Alex Hall wrote: > def foo(a=17, b=42,, x=delayed randint(0,9), y=delayed randrange(1,100)): > >> if something: >> # The simple case is realizing a direct delayed >> val = concretize x >> elif something_else: >> # This line creates

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread David Mertz
> > Where do we draw the line? How much of the behaviour of the > > function do we want to move into the header? > > Well obviously we don't draw the line until we've fallen down the > slippery slope and the entire body of the function is part of the > header! > Sure, what's the problem? :-) def

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Alex Hall
On Fri, May 29, 2020 at 7:05 PM David Mertz wrote: > On Fri, May 29, 2020 at 3:19 AM Stephen J. Turnbull < > turnbull.stephen...@u.tsukuba.ac.jp> wrote: > >> # Just too ugly for me! >> def foo(x=lambda: random.randint(0,9)): >> >> x = x() >> # ... >> > > I think this is a

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread David Mertz
On Fri, May 29, 2020 at 3:19 AM Stephen J. Turnbull < turnbull.stephen...@u.tsukuba.ac.jp> wrote: > # Just too ugly for me! > def foo(x=lambda: random.randint(0,9)): > > x = x() > # ... > I think this is a perfect example of where my desired "delayed" (or "deferred")

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Alex Hall
On Fri, May 29, 2020 at 2:18 PM Steven D'Aprano wrote: > On Thu, May 28, 2020 at 12:11:38PM +0200, Alex Hall wrote: > > > Consider this code: > > > > ``` > > x = 1 > > > > def foo(): > > print(x) > > x = 2 > > > > foo() > > ``` > > > > Here `print(x)` doesn't print '1', it gives

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Dominik Vilsmeier
On 29.05.20 15:09, Chris Angelico wrote: On Fri, May 29, 2020 at 10:51 PM Steven D'Aprano wrote: On Thu, May 28, 2020 at 08:04:07PM +1000, Chris Angelico wrote: If it's a language feature, then the name 'x' must be in the state of "local variable without a value". Oh ho, I see what you are

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Chris Angelico
On Fri, May 29, 2020 at 10:51 PM Steven D'Aprano wrote: > > On Thu, May 28, 2020 at 08:04:07PM +1000, Chris Angelico wrote: > > If it's a > > language feature, then the name 'x' must be in the state of "local > > variable without a value". > > Oh ho, I see what you are doing now :-) > > I'm going

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Steven D'Aprano
On Thu, May 28, 2020 at 08:04:07PM +1000, Chris Angelico wrote: > On Thu, May 28, 2020 at 8:01 PM Steven D'Aprano wrote: > > > > On Wed, May 27, 2020 at 05:03:09AM +1000, Chris Angelico wrote: > > > > > def foo(): > > > if False: x = 0 > > > # what is x now? > > > > > > There is no

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Steven D'Aprano
On Thu, May 28, 2020 at 12:11:38PM +0200, Alex Hall wrote: > Consider this code: > > ``` > x = 1 > > def foo(): > print(x) > x = 2 > > foo() > ``` > > Here `print(x)` doesn't print '1', it gives `UnboundLocalError: local > variable 'x' referenced before assignment`. It knows that `x`

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Steven D'Aprano
On Fri, May 29, 2020 at 12:36:20PM +1200, Greg Ewing wrote: > On 29/05/20 12:17 am, Richard Damon wrote: > >But default values for arguments are really part of the responsibility > >for the caller, not the called function. The classic implementation > >would be that the caller passes all of the

[Python-ideas] Re: Optional keyword arguments

2020-05-29 Thread Stephen J. Turnbull
Greg Ewing writes: > To my mind, the signature consists of information that a > static type checker would need to verify that a call is valid. > That does not include default values of arguments. That's a parsimonious and reasonable definition, and the one historically used by Emacs Lisp.

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Greg Ewing
On 29/05/20 12:17 am, Richard Damon wrote: But default values for arguments are really part of the responsibility for the caller, not the called function. The classic implementation would be that the caller passes all of the explicitly, I would say that's really a less-than-ideal

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Dominik Vilsmeier
On 28.05.20 17:44, Christopher Barker wrote: On Thu, May 28, 2020 at 3:50 AM Alex Hall mailto:alex.moj...@gmail.com>> wrote: On Thu, May 28, 2020 at 12:38 PM Greg Ewing mailto:greg.ew...@canterbury.ac.nz>> wrote: But I'm having trouble thinking of one. I can't remember

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Christopher Barker
On Thu, May 28, 2020 at 3:50 AM Alex Hall wrote: > On Thu, May 28, 2020 at 12:38 PM Greg Ewing > wrote: > >> >> But I'm having trouble thinking of one. I can't remember ever >> writing a function with a default argument value that *has* to >> be mutable and *has* to have a new one created on

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Richard Damon
On 5/28/20 3:00 AM, Greg Ewing wrote: > On 28/05/20 12:38 pm, Rob Cliffe wrote: >> why not go further (as the OP suggested as far as I recall) >> and allow the more concise >> >> def order(eggs = 4, spam ?= Spam()): >>     etc. > > That clutters up the header with things that are not

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Dan Sommers
On Thursday, May 28, 2020, at 06:47 -0400, Alex Hall wrote: > On Thu, May 28, 2020 at 12:38 PM Greg Ewing > wrote: > >> On 28/05/20 8:57 pm, Steven D'Aprano wrote: >> > The default value used by a parameter is certainly important, and one of >> > the most common reasons I call `help(func)` is

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Alex Hall
On Thu, May 28, 2020 at 12:38 PM Greg Ewing wrote: > On 28/05/20 8:57 pm, Steven D'Aprano wrote: > > The default value used by a parameter is certainly important, and one of > > the most common reasons I call `help(func)` is to see what the default > > values are. They should be in the

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Greg Ewing
On 28/05/20 8:57 pm, Steven D'Aprano wrote: The default value used by a parameter is certainly important, and one of the most common reasons I call `help(func)` is to see what the default values are. They should be in the signature, and it is an annoyance when all the signature shows is that it

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Greg Ewing
On 28/05/20 7:31 pm, Chris Angelico wrote: Is it an implementation detail that 4 will be used for eggs if it isn't passed? That feels different to me somehow. I think it has something to do with declarative vs. procedural stuff. The default value of eggs being 4 is a static fact, but creating

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Alex Hall
On Thu, May 28, 2020 at 11:57 AM Steven D'Aprano wrote: > On Wed, May 27, 2020 at 05:03:09AM +1000, Chris Angelico wrote: > > > def foo(): > > if False: x = 0 > > # what is x now? > > > > There is no *value* in x, yet x has a state. > > In Python code, no, it has no state, it's just an

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Chris Angelico
On Thu, May 28, 2020 at 8:01 PM Steven D'Aprano wrote: > > On Wed, May 27, 2020 at 05:03:09AM +1000, Chris Angelico wrote: > > > def foo(): > > if False: x = 0 > > # what is x now? > > > > There is no *value* in x, yet x has a state. > > In Python code, no, it has no state, it's just an

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Steven D'Aprano
On Wed, May 27, 2020 at 05:03:09AM +1000, Chris Angelico wrote: > def foo(): > if False: x = 0 > # what is x now? > > There is no *value* in x, yet x has a state. In Python code, no, it has no state, it's just an unbound name. That's literally a name that has nothing bound to it,

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Alex Hall
On Thu, May 28, 2020 at 9:01 AM Greg Ewing wrote: > On 28/05/20 12:38 pm, Rob Cliffe wrote: > > why not go > > further (as the OP suggested as far as I recall) > > and allow the more concise > > > > def order(eggs = 4, spam ?= Spam()): > > etc. > > That clutters up the header with

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Steven D'Aprano
On Thu, May 28, 2020 at 07:00:51PM +1200, Greg Ewing wrote: > On 28/05/20 12:38 pm, Rob Cliffe wrote: > >why not go further (as the OP suggested as far as I recall) > >and allow the more concise > > > >     def order(eggs = 4, spam ?= Spam()): > >         etc. > > That clutters up the header with

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Chris Angelico
On Thu, May 28, 2020 at 5:05 PM Greg Ewing wrote: > > On 28/05/20 12:38 pm, Rob Cliffe wrote: > > why not go > > further (as the OP suggested as far as I recall) > > and allow the more concise > > > > def order(eggs = 4, spam ?= Spam()): > > etc. > > That clutters up the header with

[Python-ideas] Re: Optional keyword arguments

2020-05-28 Thread Greg Ewing
On 28/05/20 12:38 pm, Rob Cliffe wrote: why not go further (as the OP suggested as far as I recall) and allow the more concise     def order(eggs = 4, spam ?= Spam()):         etc. That clutters up the header with things that are not part of the function's signature. All the caller needs

[Python-ideas] Re: Optional keyword arguments

2020-05-27 Thread Chris Angelico
On Thu, May 28, 2020 at 10:42 AM Rob Cliffe wrote: > > > > On 26/05/2020 20:03, Chris Angelico wrote: > > On Wed, May 27, 2020 at 2:51 AM Steven D'Aprano wrote: > >> But doing otherwise, having Undef be *not an object* but a kinda ghost > >> in the interpreter, is a huge language change and I

[Python-ideas] Re: Optional keyword arguments

2020-05-27 Thread Rob Cliffe via Python-ideas
APOLOGY: My e-mail client (Thunderbird) crashed as I hit Send; I don't know if it was actually sent. Therefore I am sending it again - sorry if it's a duplicate post. Rob Cliffe On 26/05/2020 20:03, Chris Angelico wrote: On Wed, May 27, 2020 at 2:51 AM Steven D'Aprano wrote: But doing

[Python-ideas] Re: Optional keyword arguments

2020-05-27 Thread Rob Cliffe via Python-ideas
On 26/05/2020 05:03, David Mertz wrote: On Mon, May 25, 2020, 11:56 PM Christopher Barker well, yes and no. this conversation was in the context of "None" works fine most of the time. How many functions take None as a non-sentinel value?! How many of that tiny numbers do so only

[Python-ideas] Re: Optional keyword arguments

2020-05-27 Thread Christopher Barker
On Tue, May 26, 2020 at 9:52 AM Steven D'Aprano wrote: > On Mon, May 25, 2020 at 08:54:52PM -0700, Christopher Barker wrote: > > On Mon, May 25, 2020 at 6:37 PM Steven D'Aprano > wrote: > > > > > A NOT_SPECIFIED singleton in builtins would be pretty clear. > > > > Guido's time machine

[Python-ideas] Re: Optional keyword arguments

2020-05-27 Thread Stephen J. Turnbull
Greg Ewing writes: > "Did the user supply a value for this optional argument?" is a > simple and reasonable question to ask. True. > It deserves to have a simple and direct way of answering it that > always works. "Deserves"? I wouldn't go farther than "it might be fun to have" that. In

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread David Mertz
On Tue, May 26, 2020 at 8:30 PM Greg Ewing wrote: > "Did the user supply a value for this optional argument?" is > a simple and reasonable question to ask. It deserves to have a > simple and direct way of answering it that always works. > It does so very little that I would definitely not want

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Greg Ewing
On 27/05/20 7:36 am, Alex Hall wrote: the only benefit I'm seeing is being able to avoid typing `if obj is sentinel`. In fact it saves even less typing than other proposals since you still have to write `obj ?= value`. "Did the user supply a value for this optional argument?" is a simple and

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread MRAB
On 2020-05-26 20:22, Chris Angelico wrote: On Wed, May 27, 2020 at 5:19 AM Alex Hall wrote: On Tue, May 26, 2020 at 9:05 PM Chris Angelico wrote: And the "is this name bound" check would potentially have other value, too. Can you think of any examples? When you're looping, searching

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Alex Hall
On Tue, May 26, 2020 at 9:24 PM Chris Angelico wrote: > On Wed, May 27, 2020 at 5:19 AM Alex Hall wrote: > > > > On Tue, May 26, 2020 at 9:05 PM Chris Angelico wrote: > >> > >> And the "is this name bound" check would potentially > >> have other value, too. > > > > > > Can you think of any

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Chris Angelico
On Wed, May 27, 2020 at 5:19 AM Alex Hall wrote: > > On Tue, May 26, 2020 at 9:05 PM Chris Angelico wrote: >> >> And the "is this name bound" check would potentially >> have other value, too. > > > Can you think of any examples? When you're looping, searching for something, and then seeing if

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Alex Hall
On Tue, May 26, 2020 at 9:05 PM Chris Angelico wrote: > And the "is this name bound" check would potentially > have other value, too. > Can you think of any examples? ___ Python-ideas mailing list -- python-ideas@python.org To unsubscribe send an

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Chris Angelico
On Wed, May 27, 2020 at 2:51 AM Steven D'Aprano wrote: > But doing otherwise, having Undef be *not an object* but a kinda ghost > in the interpreter, is a huge language change and I doubt it would be > worth it. > But is it a huge change? I thought so too, until Greg suggested a quite plausible

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread David Mertz
On Tue, May 26, 2020 at 12:53 PM Steven D'Aprano wrote: > >>> len(Undef) > TypeError: len() takes exactly one argument (0 given) > > since len takes no default values. But if Undef is considered to be an > actual object, like any other object, we ought to get this: > JavaScript and R,

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread David Mertz
On Tue, May 26, 2020 at 12:02 PM Dominik Vilsmeier wrote: > The NumPy, deque, and lru_cache cases are all ones where None is a perfect >> sentinel and the hypothetical 'undef' syntax would have zero value. > > For both `deque` and `lru_cache` None is a sensible argument so it can't > act as a

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Steven D'Aprano
On Mon, May 25, 2020 at 08:54:52PM -0700, Christopher Barker wrote: > On Mon, May 25, 2020 at 6:37 PM Steven D'Aprano wrote: > > > > A NOT_SPECIFIED singleton in builtins would be pretty clear. > > > > > > Guido's time machine strikes again! We already have that "not specified" > > singleton

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Dominik Vilsmeier
On 26.05.20 14:10, David Mertz wrote: All of those uses, including those where you say otherwise, treat None as a sentinel. In the iter() case, the optional seconds argument is *called* 'sentinel'. Guido recently mentioned that he had forgotten the two argument form of iter(), which is indeed

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Dan Sommers
On Tuesday, May 26, 2020, at 08:10 -0400, David Mertz wrote: [...] > Well, ok functions.reduce() really does make it's own sentinel in > order to show NONE as a "plain value". So I'll grant that one case is > slightly helped by a hypothetical 'undef'. In Python, 'undef' makes me think of

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Chris Angelico
On Tue, May 26, 2020 at 10:14 PM David Mertz wrote: > > All of those uses, including those where you say otherwise, treat None as a > sentinel. In the iter() case, the optional seconds argument is *called* > 'sentinel'. Guido recently mentioned that he had forgotten the two argument > form of

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread David Mertz
All of those uses, including those where you say otherwise, treat None as a sentinel. In the iter() case, the optional seconds argument is *called* 'sentinel'. Guido recently mentioned that he had forgotten the two argument form of iter(), which is indeed funny... But useful. Well, ok

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread MRAB
On 2020-05-26 07:21, David Mertz wrote: You mean the sentinel is called 'undef'? With undef-coallescing operators? On Tue, May 26, 2020, 2:14 AM Greg Ewing > wrote: Wild idea: Instead of sentinels, have a way of declaring optional arguments with no

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Dominik Vilsmeier
On 26.05.20 06:03, David Mertz wrote: On Mon, May 25, 2020, 11:56 PM Christopher Barker well, yes and no. this conversation was in the context of "None" works fine most of the time. How many functions take None as a non-sentinel value?! How many of that tiny numbers do so only

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Chris Angelico
On Tue, May 26, 2020 at 4:25 PM David Mertz wrote: > > You mean the sentinel is called 'undef'? With undef-coallescing operators? > No, that would imply that it is a value. As I understand it, the sentinel is an *unbound local variable*. ChrisA ___

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread David Mertz
You mean the sentinel is called 'undef'? With undef-coallescing operators? On Tue, May 26, 2020, 2:14 AM Greg Ewing wrote: > Wild idea: Instead of sentinels, have a way of declaring optional > arguments with no default, and a way of conditionally assigning a > value to them if they are not

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Chris Angelico
On Tue, May 26, 2020 at 4:12 PM Greg Ewing wrote: > > Wild idea: Instead of sentinels, have a way of declaring optional > arguments with no default, and a way of conditionally assigning a > value to them if they are not bound. > > E.g. > > def order(eggs = 4, spam =): > spam ?=

[Python-ideas] Re: Optional keyword arguments

2020-05-26 Thread Greg Ewing
Wild idea: Instead of sentinels, have a way of declaring optional arguments with no default, and a way of conditionally assigning a value to them if they are not bound. E.g. def order(eggs = 4, spam =): spam ?= Spam() Here the '?=' means "if spam is not bound, then evaluate the rhs

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread David Mertz
On Mon, May 25, 2020, 11:56 PM Christopher Barker > well, yes and no. this conversation was in the context of "None" works > fine most of the time. > How many functions take None as a non-sentinel value?! How many of that tiny numbers do so only because they are poorly designed. None already is

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Christopher Barker
On Mon, May 25, 2020 at 6:37 PM Steven D'Aprano wrote: > > A NOT_SPECIFIED singleton in builtins would be pretty clear. > > Guido's time machine strikes again! We already have that "not specified" > singleton in the builtins, with a nice repr. It's spelled "None". > well, yes and no. this

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Mon, May 25, 2020 at 02:19:57PM -0700, Christopher Barker wrote: > > It's an extremely common idiom right up until it doesn't > > work, and you need: > > > > _SENTINEL = object() > > def fun(..., option=_SENTINEL): > > if option is _SENTINEL: > > option = something_else > > > >

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Chris Angelico
On Tue, May 26, 2020 at 11:05 AM Steven D'Aprano wrote: > If you want the default value to be X, then: > > - if you want a fresh X each time, then you want late binding; > > - if you want the same X each time, then you want early binding; > > - if you don't care, then early binding is likely to

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Tue, May 26, 2020 at 02:25:38AM +0300, Paul Sokolovsky wrote: > > > I'd suggest that people should love "explicit is better than > > > implicit" principle of the language. > > > > Explicit meaning that you need to use a specific symbol that means > > "this is to be late-bound"? > > No, it

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Mon, May 25, 2020 at 06:45:47PM -0400, Eric V. Smith wrote: > On 5/25/2020 6:37 PM, Chris Angelico wrote: > >Explicit meaning that you need to use a specific symbol that means > >"this is to be late-bound"? > > > >Or explicit meaning "something that I like", as opposed to implicit > >meaning

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Paul Sokolovsky
Hello, On Tue, 26 May 2020 08:37:59 +1000 Chris Angelico wrote: [] > > def foo(x := a + b) > > > > vs > > > > c = a + b > > def foo(x := c) > > > > can lead to different results. > > def foo(x = None): > if x is None: x = a + b > > c = a + b > def foo(x = None):

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Eric V. Smith
On 5/25/2020 6:37 PM, Chris Angelico wrote: Explicit meaning that you need to use a specific symbol that means "this is to be late-bound"? Or explicit meaning "something that I like", as opposed to implicit meaning "something that I don't like", which is how most people seem to interpret that

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Chris Angelico
On Tue, May 26, 2020 at 8:06 AM Paul Sokolovsky wrote: > > Hello, > > The more worrying sounds the idea to have a special evaluation context > for specially marked default arguments. Just imagine - *everywhere* (#1) > in the language the evaluation is eager, while for specially marked > default

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Paul Sokolovsky
Hello, On Tue, 26 May 2020 07:34:32 +1000 Chris Angelico wrote: [] > > _complex_mutable_default = MyObj(a = 1, b = 2, c = 3, d = 4, e = 5, > > f = 6, g = 7, h = 8, i = 9, j = 10) > > > > def func(x := _complex_mutable_default): ... > > > > Above, _complex_mutable_default is NOT immutable-- it

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Chris Angelico
On Tue, May 26, 2020 at 7:19 AM Ricky Teachey wrote: > > This late binding is what I was clumsily referring to as option 3 (version > with no copying). But this is still going to end up having what people would > consider surprising behavior, is it not? > > It is essentially equivalent to this

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Christopher Barker
On Sun, May 24, 2020 at 12:55 PM Chris Angelico wrote: > > And it isn't entirely correct, because now None isn't a valid > parameter. Which is indeed, an extremely common use case :-) > It's an extremely common idiom right up until it doesn't > work, and you need: > > _SENTINEL = object() >

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Ricky Teachey
On Mon, May 25, 2020 at 12:33 PM Chris Angelico wrote: > On Tue, May 26, 2020 at 2:24 AM Dominik Vilsmeier > wrote: > > > > On 25.05.20 17:29, Ricky Teachey wrote: > > > > > > On Mon, May 25, 2020, 6:49 AM Rob Cliffe via Python-ideas < > python-ideas@python.org> wrote: > >> > >> (Possibly

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Tue, May 26, 2020 at 02:36:16AM +1000, Steven D'Aprano wrote: > On Mon, May 25, 2020 at 02:03:49AM +0100, Rob Cliffe via Python-ideas wrote: > > [quoting Dominik Vilsmeier] > > >It looks like the most common use case for this is to deal with > > >mutable defaults > > It might be the most

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Mon, May 25, 2020 at 06:22:14PM +0200, Dominik Vilsmeier wrote: > It wouldn't copy the provided default, it would just reevaluate the > expression. Python has already a way of deferring evaluation, generator > expressions: > >     >>> x = 1 >     >>> g = (x for __ in range(2)) >     >>>

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Dominik Vilsmeier
On 25.05.20 18:29, Chris Angelico wrote: On Tue, May 26, 2020 at 2:24 AM Dominik Vilsmeier wrote: On 25.05.20 17:29, Ricky Teachey wrote: On Mon, May 25, 2020, 6:49 AM Rob Cliffe via Python-ideas wrote: (Possibly heretical) Thought: ISTM that when the decision was made that arg default

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Mon, May 25, 2020 at 02:03:49AM +0100, Rob Cliffe via Python-ideas wrote: [quoting Dominik Vilsmeier] > >It looks like the most common use case for this is to deal with > >mutable defaults It might be the most common use case, but it's hardly the only use case. The PEP talks about special

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Alex Hall
On Mon, May 25, 2020 at 6:24 PM Dominik Vilsmeier wrote: > On 25.05.20 17:29, Ricky Teachey wrote: > > > On Mon, May 25, 2020, 6:49 AM Rob Cliffe via Python-ideas < > python-ideas@python.org> wrote: > >> (Possibly heretical) Thought: >> ISTM that when the decision was made that arg default

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Chris Angelico
On Tue, May 26, 2020 at 2:24 AM Dominik Vilsmeier wrote: > > On 25.05.20 17:29, Ricky Teachey wrote: > > > On Mon, May 25, 2020, 6:49 AM Rob Cliffe via Python-ideas > wrote: >> >> (Possibly heretical) Thought: >> ISTM that when the decision was made that arg default values should be >>

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Dominik Vilsmeier
On 25.05.20 17:29, Ricky Teachey wrote: On Mon, May 25, 2020, 6:49 AM Rob Cliffe via Python-ideas mailto:python-ideas@python.org>> wrote:  (Possibly heretical) Thought: ISTM that when the decision was made that arg default values should be evaluated         once, at function

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Ricky Teachey
On Mon, May 25, 2020, 6:49 AM Rob Cliffe via Python-ideas < python-ideas@python.org> wrote: > (Possibly heretical) Thought: > ISTM that when the decision was made that arg default values should be > evaluated > once, at function definition time, > rather than > every time the

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Steven D'Aprano
On Sun, May 24, 2020 at 01:38:26PM -0400, David Mertz wrote: > The pattern: > > def fun(..., option=None): > if option is None: > option = something_else > > Becomes second nature very quickly. Once you learn it, you know it. A > line of two of code isn't a big deal. Sure, but

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Dominik Vilsmeier
On 25.05.20 03:03, Rob Cliffe via Python-ideas wrote: On 24/05/2020 21:03, Dominik Vilsmeier wrote: On 24.05.20 18:34, Alex Hall wrote: OK, let's forget the colon. The point is just to have some kind of 'modifier' on the default value to say 'this is evaluated on each function call',

[Python-ideas] Re: Optional keyword arguments

2020-05-25 Thread Rob Cliffe via Python-ideas
On 24/05/2020 21:03, Dominik Vilsmeier wrote: On 24.05.20 18:34, Alex Hall wrote: OK, let's forget the colon. The point is just to have some kind of 'modifier' on the default value to say 'this is evaluated on each function call', while still having something that looks like `arg=`.

[Python-ideas] Re: Optional keyword arguments

2020-05-24 Thread Alex Hall
On Sun, May 24, 2020 at 10:05 PM Dominik Vilsmeier wrote: > On 24.05.20 18:34, Alex Hall wrote: > > > OK, let's forget the colon. The point is just to have some kind of > 'modifier' on the default value to say 'this is evaluated on each function > call', while still having something that looks

[Python-ideas] Re: Optional keyword arguments

2020-05-24 Thread Dominik Vilsmeier
On 24.05.20 19:38, David Mertz wrote: As syntax, I presume this would be something like: output = [] for x in data:     a = delayed inc(x)     b = delayed double(x)     c = delayed add(a, b) output.append(c) total = sum(outputs)  # concrete answer here. Obviously the simple example of adding

[Python-ideas] Re: Optional keyword arguments

2020-05-24 Thread Dominik Vilsmeier
On 24.05.20 18:34, Alex Hall wrote: OK, let's forget the colon. The point is just to have some kind of 'modifier' on the default value to say 'this is evaluated on each function call', while still having something that looks like `arg=`. Maybe something like:      def func(options=from

[Python-ideas] Re: Optional keyword arguments

2020-05-24 Thread Chris Angelico
On Mon, May 25, 2020 at 3:42 AM David Mertz wrote: > The pattern: > > def fun(..., option=None): > if option is None: > option = something_else > > Becomes second nature very quickly. Once you learn it, you know it. A line > of two of code isn't a big deal. > And it isn't entirely

  1   2   >