Re: what does 'a=b=c=[]' do

2011-12-25 Thread Steven D'Aprano
On Sat, 24 Dec 2011 19:41:55 +0100, Thomas Rachel wrote: >> The only times you need the brackets around a tuple is to control the >> precedence of operations, or for an empty tuple. > > IBTD: > > a=((a, b) for a, b, c in some_iter) > b=[(1, c) for ] > > Without the round brackets, it is a synta

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: what does 'a=b=c=[]' do

2011-12-24 Thread Thomas Rachel
Am 22.12.2011 00:20 schrieb Dennis Lee Bieber: The key one is that lists ([] defines a list, not an array) are "mutable". Your "7" is not mutable. Strictly spoken, that's only a "side show" where the effect is visible. The real key concept is that [] creates *one* object which is then

Re: what does 'a=b=c=[]' do

2011-12-24 Thread Thomas Rachel
Am 22.12.2011 00:48 schrieb Steven D'Aprano: On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote: For the amount of typing, it's easier to just do a straight line tuple unpack a,b,c = ([],[],[]) Note that tuples are created by the comma, not the round brackets (or parenthese

Re: what does 'a=b=c=[]' do

2011-12-24 Thread Thomas Rachel
Am 21.12.2011 23:25 schrieb Eric: Is it true that if I want to create an array or arbitrary size such as: for a in range(n): x.append() I must do this instead? x=[] for a in range(n): x.append() Of course - your x must exist before using it. > Now to my actual quest

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: what does 'a=b=c=[]' do

2011-12-24 Thread Lie Ryan
On 12/22/2011 10:20 AM, Dennis Lee Bieber wrote: which is to define the names "a", "b", and "c", and connects the three names to the single object (integer 7 or new empty list). note that this "connects" and "disconnecting" business is more commonly referred to in python parlance as "binding"

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: what does 'a=b=c=[]' do

2011-12-23 Thread Michael Torrie
On 12/23/2011 03:31 AM, rusi wrote: > In Fortran, if the comma in the loop > DO 10 I = 1,10 > is misspelt as '.' it becomes the assignment > DO10I = 1.0 > > Do you consider it a bug or a feature? > Does Fortran consider it a bug or feature? Non sequitor. Nothing at all to do with the issue at ha

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: what does 'a=b=c=[]' do

2011-12-23 Thread rusi
On Dec 23, 8:33 pm, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote: > > On Dec 23, 6:53 pm, Robert Kern wrote: > >> On 12/23/11 1:23 PM, rusi wrote: > [...] > >> > Of course it should be fixed.  The repeated recurrence of it as a > >> > standard gotcha as well as the pyth

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

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Steven D'Aprano
On Fri, 23 Dec 2011 06:57:02 -0800, rusi wrote: > On Dec 23, 6:53 pm, Robert Kern wrote: >> On 12/23/11 1:23 PM, rusi wrote: [...] >> > Of course it should be fixed.  The repeated recurrence of it as a >> > standard gotcha as well as the python ideas list testifies to that. >> >> So you were lyin

Re: what does 'a=b=c=[]' do

2011-12-23 Thread rusi
On Dec 23, 6:53 pm, Robert Kern wrote: > On 12/23/11 1:23 PM, rusi wrote: > > > > > > > > > > > On Dec 23, 6:10 pm, Robert Kern  wrote: > >> On 12/23/11 10:22 AM, rusi wrote: > >>> On Dec 23, 2:39 pm, Steven D'Aprano >>> +comp.lang.pyt...@pearwood.info>    wrote: > Some people might argue tha

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Robert Kern
On 12/23/11 1:23 PM, rusi wrote: On Dec 23, 6:10 pm, Robert Kern wrote: On 12/23/11 10:22 AM, rusi wrote: On Dec 23, 2:39 pm, Steven D'Apranowrote: Some people might argue that it is a mistake, a minor feature which allegedly causes more difficulties than benefits. I do not hold with th

Re: what does 'a=b=c=[]' do

2011-12-23 Thread rusi
On Dec 23, 6:10 pm, Robert Kern wrote: > On 12/23/11 10:22 AM, rusi wrote: > > > > > > > > > > > On Dec 23, 2:39 pm, Steven D'Aprano > +comp.lang.pyt...@pearwood.info>  wrote: > >> On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > >>> Likewise function arguments that default to mutable entities is

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Neil Cerutti
On 2011-12-23, Chris Angelico wrote: > On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: >> That is the most ridiculous thing I have heard in a while. >> ?Mutable default arguments are *not* a bug in Python. >> >> Reminds me of a bug report a couple years back claiming >> multiple inheritence

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Neil Cerutti
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 existing code. ;) -- Neil Cerutti -- http://mail.python.org/mailma

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Robert Kern
On 12/23/11 10:22 AM, rusi wrote: On Dec 23, 2:39 pm, Steven D'Aprano wrote: On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: Likewise function arguments that default to mutable entities is a known gotcha of python which is best treated as a bug in python. Nonsense. It is a feature, not a bug

Re: what does 'a=b=c=[]' do

2011-12-23 Thread rusi
On Dec 23, 2:39 pm, Steven D'Aprano wrote: > On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > > Likewise function arguments that default to mutable entities is a known > > gotcha of python which is best treated as a bug in python. > > Nonsense. It is a feature, not a bug. Tsk Tsk How can python

Re: what does 'a=b=c=[]' do

2011-12-23 Thread rusi
On Dec 23, 2:59 pm, Chris Angelico wrote: > On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: > > That is the most ridiculous thing I have heard in a while.  Mutable default > > arguments are *not* a bug in Python. > > > Reminds me of a bug report a couple years back claiming multiple inherite

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Chris Angelico
On Fri, Dec 23, 2011 at 7:49 PM, Ethan Furman wrote: > That is the most ridiculous thing I have heard in a while.  Mutable default > arguments are *not* a bug in Python. > > Reminds me of a bug report a couple years back claiming multiple inheritence > was a bug and asking it to be removed. Both

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Ethan Furman
rusi wrote: On Dec 23, 7:10 am, alex23 wrote: On Dec 22, 6:51 pm, Rolf Camps wrote: I'm afraid it's dangerous to encourage the use of '[]' as assignment to a parameter in a function definition. If you use the function several times 'default' always points to the same list. >> I appreciate

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Steven D'Aprano
On Fri, 23 Dec 2011 00:38:07 -0800, rusi wrote: > Likewise function arguments that default to mutable entities is a known > gotcha of python which is best treated as a bug in python. Nonsense. It is a feature, not a bug. Some people might argue that it is a mistake, a minor feature which allege

Re: what does 'a=b=c=[]' do

2011-12-23 Thread Ethan Furman
Ian Kelly wrote: On Thu, Dec 22, 2011 at 7:10 PM, alex23 wrote: On Dec 22, 6:51 pm, Rolf Camps wrote: I'm afraid it's dangerous to encourage the use of '[]' as assignment to a parameter in a function definition. If you use the function several times 'default' always points to the same list.

Re: what does 'a=b=c=[]' do

2011-12-23 Thread rusi
On Dec 23, 7:10 am, alex23 wrote: > On Dec 22, 6:51 pm, Rolf Camps wrote: > > > I'm afraid it's dangerous to encourage the use of '[]' as assignment to > > a parameter in a function definition. If you use the function several > > times 'default' always points to the same list. > > I appreciate th

Re: what does 'a=b=c=[]' do

2011-12-22 Thread alex23
On Dec 23, 3:22 pm, Ian Kelly wrote: > Nobody is asking you to modify your coding style.  The request is that > you not throw it up as an example without mentioning the important > caveats. No, 100% no. It's not my responsibility to mention every potentially relevant gotcha when providing example

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Ian Kelly
On Thu, Dec 22, 2011 at 8:40 PM, alex23 wrote: > On Dec 23, 12:59 pm, Ian Kelly wrote: >> It's only irrelevant in the immediate context of the code you posted. >> But when Joe Novice sees your code and likes it and duplicates it a >> million times > > I'm sorry, but I'm not going to modify my cod

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Eric
On Dec 21, 5:44 pm, Steven D'Aprano wrote: > Yes, you should create your lists before trying to append to them. > > But you aren't forced to use a for-loop. You can use a list comprehension: > > x = [some_function(a) for a in range(n)] > > Notice that here you don't need x to pre-exist, because t

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Chris Angelico
On Fri, Dec 23, 2011 at 2:40 PM, alex23 wrote: > I'm sorry, but I'm not going to modify my coding style for the sake of > bad programmers. And there, folks, you have one of the eternal dilemmas. The correct decision depends on myriad factors; if you're writing code to go into the documentation as

Re: what does 'a=b=c=[]' do

2011-12-22 Thread alex23
On Dec 23, 12:59 pm, Ian Kelly wrote: > It's only irrelevant in the immediate context of the code you posted. > But when Joe Novice sees your code and likes it and duplicates it a > million times I'm sorry, but I'm not going to modify my coding style for the sake of bad programmers. The context

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Eric
On Dec 21, 6:50 pm, alex23 wrote: > On Dec 22, 8:25 am, Eric wrote: > > > This surprises me, can someone tell me why it shouldn't?  I figure if > > I want to create and initialize three scalars the just do "a=b=c=7", > > for example, so why not extend it to arrays. > > The thing to remember is th

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Ian Kelly
On Thu, Dec 22, 2011 at 7:10 PM, alex23 wrote: > On Dec 22, 6:51 pm, Rolf Camps wrote: >> I'm afraid it's dangerous to encourage the use of '[]' as assignment to >> a parameter in a function definition. If you use the function several >> times 'default' always points to the same list. > > I appre

Re: what does 'a=b=c=[]' do

2011-12-22 Thread alex23
On Dec 22, 6:51 pm, Rolf Camps wrote: > I'm afraid it's dangerous to encourage the use of '[]' as assignment to > a parameter in a function definition. If you use the function several > times 'default' always points to the same list. I appreciate the concern, but adding a default argument guard w

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Ethan Furman
Rolf Camps wrote: alex23 schreef op wo 21-12-2011 om 16:50 [-0800]: I'd say that _is_ the most pythonic way, it's very obvious in its intent (or would be with appropriate names). If it bothers you that much: def listgen(count, default=[]): for _ in xrange(count): yield d

Re: what does 'a=b=c=[]' do

2011-12-22 Thread Rolf Camps
alex23 schreef op wo 21-12-2011 om 16:50 [-0800]: > On Dec 22, 8:25 am, Eric wrote: > > This surprises me, can someone tell me why it shouldn't? I figure if > > I want to create and initialize three scalars the just do "a=b=c=7", > > for example, so why not extend it to arrays. > > The thing to

Re: what does 'a=b=c=[]' do

2011-12-21 Thread alex23
On Dec 22, 8:25 am, Eric wrote: > This surprises me, can someone tell me why it shouldn't?  I figure if > I want to create and initialize three scalars the just do "a=b=c=7", > for example, so why not extend it to arrays. The thing to remember is that everything is an object, and that it's better

Re: what does 'a=b=c=[]' do

2011-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2011 18:20:16 -0500, Dennis Lee Bieber wrote: > For the amount of typing, it's easier to just do a straight line > tuple unpack > a,b,c = ([],[],[]) Note that tuples are created by the comma, not the round brackets (or parentheses for any Americans reading). So the rou

Re: what does 'a=b=c=[]' do

2011-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2011 14:25:17 -0800, Eric wrote: > Is it true that if I want to create an array or arbitrary size such as: >for a in range(n): > x.append() x is not defined, so you will get a NameError unless by some lucky fluke something else has created x AND it happens to be a list.

Re: what does 'a=b=c=[]' do

2011-12-21 Thread Chris Kaynor
On Wed, Dec 21, 2011 at 2:25 PM, Eric wrote: > > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() You can also use a list comprehension: x = [ for a in

Re: what does 'a=b=c=[]' do

2011-12-21 Thread Joshua Landau
On 21 December 2011 22:25, Eric wrote: > Is it true that if I want to create an array or arbitrary size such > as: > for a in range(n): > x.append() > > I must do this instead? > x=[] > for a in range(n): > x.append() > > Now to my actual question. I need to do the above for mult

what does 'a=b=c=[]' do

2011-12-21 Thread Eric
Is it true that if I want to create an array or arbitrary size such as: for a in range(n): x.append() I must do this instead? x=[] for a in range(n): x.append() Now to my actual question. I need to do the above for multiple arrays (all the same, arbitrary size). So I do thi