Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Lie Ryan
On 12/24/2011 07:25 PM, Steven D'Aprano wrote: I'd use a function attribute. def func(x, y=None): if y is None: y = func.default_y ... func.default_y = [] That's awkward only if you believe function attributes are awkward. I do. All you've done is move the default from *before* the

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread rusi
On Dec 25, 5:32 am, Devin Jeanpierre wrote: > alex23 wrote: > > Because I believe that the source of confusion has far more to do with > > mutable/immutable objects than with early/late binding. Masking or > > 'correcting' an aspect of Python's behaviour because novices make the > > wrong assumpti

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Devin Jeanpierre
> Because I believe that the source of confusion has far more to do with > mutable/immutable objects than with early/late binding. Masking or > 'correcting' an aspect of Python's behaviour because novices make the > wrong assumption about it just pushes the problem elsewhere and > potentially makes

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread alex23
On Dec 25, 9:25 am, Devin Jeanpierre wrote: > > If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > > see an increase in the number of questions like the OP's. > > What makes you so sure? Both models do make sense and are equally > valid, it's just that only one of them is tru

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Devin Jeanpierre
> If Python was ever 'fixed' to prevent this issue, I'm pretty sure we'd > see an increase in the number of questions like the OP's. What makes you so sure? Both models do make sense and are equally valid, it's just that only one of them is true. Is it just because people already used to Python wo

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread alex23
On Dec 24, 6:25 pm, Steven D'Aprano wrote: > > It's > > much harder to figure out what's going wrong with an early-bound > > mutable. > > Only for those who don't understand, or aren't thinking about, Python's > object model. The behaviour of early-bound mutables is obvious and clear > once you th

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread alex23
On Dec 24, 2:15 am, Roy Smith wrote: > I know this is not quite the same thing, but it's interesting to look at > what django (and mongoengine) do in their model definitions, prompted by > your time.time() example.  You can do declare a model field something > like: > > class Foo(models.Model): >

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread alex23
On Dec 24, 2:27 am, Mel Wilson wrote: > In a tool that's meant for other people to use to accomplish work of their > own, breaking workflow is a cardinal sin. > > In a research language that's meant always to be up-to-date with the concept > of the week, not so much. What on earth gave you the im

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Steven D'Aprano
On Fri, 23 Dec 2011 19:24:44 -0500, Devin Jeanpierre wrote: >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body of the function, usually in a global >> variable: >> >>

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Steven D'Aprano
On Fri, 23 Dec 2011 17:03:11 +, Neil Cerutti wrote: >> The disadvantage of late binding is that since the expression is live, >> it needs to be calculated each time, even if it turns out to be the >> same result. But there's no guarantee that it will return the same >> result each time: > > T

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-24 Thread Steven D'Aprano
On Sat, 24 Dec 2011 09:50:04 +1100, Chris Angelico wrote: > On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano > wrote: >> Yes. But having to manage it *by hand* is still unclean: > > Well, my point was that Python's current behaviour _is_ that. Minus the managing it by hand part. >> * you stil

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Devin Jeanpierre
> To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > >_DEFAULT_Y = [] # Private constant, don't touch. > >def func(x,

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Chris Angelico
On Sat, Dec 24, 2011 at 9:32 AM, Steven D'Aprano wrote: > Yes. But having to manage it *by hand* is still unclean: Well, my point was that Python's current behaviour _is_ that. > * you still have to assign the default value to the function assignment > outside the function, which is inelegant;

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Steven D'Aprano
On Sat, 24 Dec 2011 02:55:41 +1100, Chris Angelico wrote: > On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano > wrote: >> To fake early binding when the language provides late binding, you >> still use a sentinel value, but the initialization code creating the >> default value is outside the body

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Neil Cerutti
On 2011-12-23, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +, Neil Cerutti wrote: >> On 2011-12-23, Neil Cerutti wrote: >>> Is the misfeature that Python doesn't evaluate the default >>> argument expression every time you call the function? What >>> would be the harm if it did? >>

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Mel Wilson
Steven D'Aprano wrote: > On Fri, 23 Dec 2011 13:13:38 +, Neil Cerutti wrote: >> On 2011-12-23, Neil Cerutti wrote: >> ...you know, assuming it wouldn't break existing code. ;) > > It will. Python's default argument strategy has been in use for 20 years. > Some code will rely on it. I know min

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Roy Smith
In article <4ef4a30d$0$29973$c3e8da3$54964...@news.astraweb.com>, Steven D'Aprano wrote: > The disadvantage of late binding is that since the expression is live, it > needs to be calculated each time, even if it turns out to be the same > result. But there's no guarantee that it will return th

Re: Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Chris Angelico
On Sat, Dec 24, 2011 at 2:49 AM, Steven D'Aprano wrote: > To fake early binding when the language provides late binding, you still > use a sentinel value, but the initialization code creating the default > value is outside the body of the function, usually in a global variable: > >    _DEFAULT_Y =

Early and late binding [was Re: what does 'a=b=c=[]' do]

2011-12-23 Thread Steven D'Aprano
On Fri, 23 Dec 2011 13:13:38 +, Neil Cerutti wrote: > On 2011-12-23, Neil Cerutti wrote: >> Is the misfeature that Python doesn't evaluate the default argument >> expression every time you call the function? What would be the harm if >> it did? > > ...you know, assuming it wouldn't break exi