Re: functions, optional parameters

2015-05-10 Thread Chris Angelico
On Sun, May 10, 2015 at 9:25 PM, Dave Angel wrote: > On 05/09/2015 11:33 PM, Chris Angelico wrote: >> What you could have is "late-binding semantics, optional early binding >> as an optimization but only in cases where the result is >> indistinguishable". That would allow common cases (int/bool/st

Re: functions, optional parameters

2015-05-10 Thread Dave Angel
On 05/09/2015 11:33 PM, Chris Angelico wrote: On Sun, May 10, 2015 at 12:45 PM, Steven D'Aprano wrote: This is the point where some people try to suggest some sort of complicated, fragile, DWIM heuristic where the compiler tries to guess whether the user actually wants the default to use early

Re: functions, optional parameters

2015-05-10 Thread Chris Angelico
(To clarify, I am *not* talking about this as a change to Python, so all questions of backward compatibility are immaterial. This is "what happens if we go back in time and have Python use late binding semantics". This is the "alternate 1985" of Back to the Future.) On Sun, May 10, 2015 at 3:20 PM

Re: functions, optional parameters

2015-05-09 Thread Steven D'Aprano
On Sun, 10 May 2015 01:35 pm, Rustom Mody wrote: > On Sunday, May 10, 2015 at 8:16:07 AM UTC+5:30, Steven D'Aprano wrote: >> I predict that the majority of the time, late binding would just be a >> pointless waste of time: >> >> def process_string(thestr, start=0, end=None, slice=1, reverse=True)

Re: functions, optional parameters

2015-05-09 Thread Steven D'Aprano
On Sun, 10 May 2015 01:33 pm, Chris Angelico wrote: > On Sun, May 10, 2015 at 12:45 PM, Steven D'Aprano > wrote: >> This is the point where some people try to suggest some sort of >> complicated, fragile, DWIM heuristic where the compiler tries to guess >> whether the user actually wants the defa

Re: functions, optional parameters

2015-05-09 Thread Rustom Mody
On Sunday, May 10, 2015 at 8:16:07 AM UTC+5:30, Steven D'Aprano wrote: > I predict that the majority of the time, late binding would just be a > pointless waste of time: > > def process_string(thestr, start=0, end=None, slice=1, reverse=True): > pass > > Why would you want 0, None, 1 and True

Re: functions, optional parameters

2015-05-09 Thread Chris Angelico
On Sun, May 10, 2015 at 12:45 PM, Steven D'Aprano wrote: > This is the point where some people try to suggest some sort of complicated, > fragile, DWIM heuristic where the compiler tries to guess whether the user > actually wants the default to use early or late binding, based on what the > expres

Re: functions, optional parameters

2015-05-09 Thread Steven D'Aprano
On Sat, 9 May 2015 01:50 am, Michael Welle wrote: [...] >> How about this definition: >> >> default = 23 >> def spam(eggs=default): >> pass >> >> del default >> >> print spam() >> >> >> Do you expect the function call to fail because `default` doesn't exist? > > If I refer

Re: functions, optional parameters

2015-05-09 Thread Ian Kelly
On Fri, May 8, 2015 at 9:50 AM, Michael Welle wrote: > > Steven D'Aprano writes: >> >> If your language uses late binding, it is very inconvenient to get early >> binding when you want it. But if your language uses early binding, it is >> very simple to get late binding when you want it: just put

Re: functions, optional parameters

2015-05-09 Thread Gregory Ewing
Chris Angelico wrote: So no, it isn't proof - it's equally well explained by the code object being constant. I suppose, strictly speaking, that's true -- but then the code object *might as well* be created at compile time, since the semantics are identical. In any case, it's easy to see from

Re: functions, optional parameters

2015-05-08 Thread Steven D'Aprano
On Sat, 9 May 2015 03:49 am, Chris Angelico wrote: > On Sat, May 9, 2015 at 3:36 AM, Steven D'Aprano > wrote: >> On Sat, 9 May 2015 02:02 am, Chris Angelico wrote: >>> Aside from constructing two closures in the same context and proving >>> that their __code__ attributes point to the same object,

Re: functions, optional parameters

2015-05-08 Thread Chris Angelico
On Sat, May 9, 2015 at 11:41 AM, Gregory Ewing wrote: > Chris Angelico wrote: >> >> How do you know that the function's code >> object was created when compile() happened, rather than being created >> when the function was defined? > > > Python 3.4.2 (default, Feb 4 2015, 20:08:25) > [GCC 4.2.1 (

Re: functions, optional parameters

2015-05-08 Thread Gregory Ewing
Chris Angelico wrote: How do you know that the function's code object was created when compile() happened, rather than being created when the function was defined? Python 3.4.2 (default, Feb 4 2015, 20:08:25) [GCC 4.2.1 (Apple Inc. build 5664)] on darwin Type "help", "copyright", "credits" or

Re: functions, optional parameters

2015-05-08 Thread Mel Wilson
On Sat, 09 May 2015 03:49:36 +1000, Chris Angelico wrote: > Yes, but can you *distinguish* them in terms of default argument versus > code object creation? How do you know that the function's code object > was created when compile() happened, rather than being created when the > function was defin

Re: functions, optional parameters

2015-05-08 Thread Chris Angelico
On Sat, May 9, 2015 at 3:36 AM, Steven D'Aprano wrote: > On Sat, 9 May 2015 02:02 am, Chris Angelico wrote: >> Aside from constructing two closures in the same context and proving >> that their __code__ attributes point to the same object, is there any >> way to distinguish between "code object co

Re: functions, optional parameters

2015-05-08 Thread Steven D'Aprano
On Sat, 9 May 2015 02:02 am, Chris Angelico wrote: > On Sat, May 9, 2015 at 1:48 AM, Ian Kelly wrote: >> On May 8, 2015 9:26 AM, "Steven D'Aprano" >> wrote: >>> >>> Do you think that Python will re-compile the body of the function every >>> time >>> you call it? Setting the default is part of th

Re: functions, optional parameters

2015-05-08 Thread Steven D'Aprano
On Sat, 9 May 2015 01:48 am, Ian Kelly wrote: > On May 8, 2015 9:26 AM, "Steven D'Aprano" < > steve+comp.lang.pyt...@pearwood.info> wrote: >> >> Do you think that Python will re-compile the body of the function every > time >> you call it? Setting the default is part of the process of compiling th

Re: functions, optional parameters

2015-05-08 Thread Chris Angelico
On Sat, May 9, 2015 at 1:48 AM, Ian Kelly wrote: > On May 8, 2015 9:26 AM, "Steven D'Aprano" > wrote: >> >> Do you think that Python will re-compile the body of the function every >> time >> you call it? Setting the default is part of the process of compiling the >> function. > > To be a bit peda

Re: functions, optional parameters

2015-05-08 Thread Ian Kelly
On May 8, 2015 9:26 AM, "Steven D'Aprano" < steve+comp.lang.pyt...@pearwood.info> wrote: > > Do you think that Python will re-compile the body of the function every time > you call it? Setting the default is part of the process of compiling the > function. To be a bit pedantic, that's not accurate

Re: functions, optional parameters

2015-05-08 Thread Steven D'Aprano
On Fri, 8 May 2015 09:59 pm, Michael Welle wrote: > Hello, > > assume the following function definition: > > def bar(foo = []): > print("foo: %s" % foo) > foo.append("foo") > > It doesn't work like one would expect (or as I would expect ;-)). As I > understand it the assignment of the e

Re: functions, optional parameters

2015-05-08 Thread Chris Angelico
On Fri, May 8, 2015 at 9:59 PM, Michael Welle wrote: > Hello, > > assume the following function definition: > > def bar(foo = []): > print("foo: %s" % foo) > foo.append("foo") > > It doesn't work like one would expect (or as I would expect ;-)). As I > understand it the assignment of the e

Re: functions, optional parameters

2015-05-08 Thread Rustom Mody
On Friday, May 8, 2015 at 5:30:15 PM UTC+5:30, Michael Welle wrote: > Hello, > > assume the following function definition: > > def bar(foo = []): > print("foo: %s" % foo) > foo.append("foo") > > It doesn't work like one would expect (or as I would expect ;-)). As I > understand it the as