Re: bad recursion, still works

2008-07-17 Thread mdsherry
On Jul 17, 8:27 am, Jeff <[EMAIL PROTECTED]> wrote: > Thanks, that made things very clear.  I like that technique for adding > memoization via the parameter.  That is clever.  It would be nice if > there were a way to have multiple functions close over a shared local > variable in Python, like let-

Re: bad recursion, still works

2008-07-17 Thread Jeff
Thanks, that made things very clear. I like that technique for adding memoization via the parameter. That is clever. It would be nice if there were a way to have multiple functions close over a shared local variable in Python, like let-binding in lisp. -- http://mail.python.org/mailman/listinfo/

Re: bad recursion, still works

2008-07-16 Thread Peter Pearson
On Wed, 16 Jul 2008 15:20:23 +0200, Fredrik Lundh wrote: [snip] > Hope this helps more than it confuses. Absolutely. It is wonderfully enlightening. Many thanks. -- To email me, substitute nowhere->spamcop, invalid->net. -- http://mail.python.org/mailman/listinfo/python-list

Re: bad recursion, still works

2008-07-16 Thread Fredrik Lundh
Jeff wrote: Is this avoidable by using a call to list() in the definition instead? No. Default values are *always* evaluated when, and only when, the "def" statement is executed; see: http://docs.python.org/ref/function.html Also note that "def" is an executable statement in Python, a

Re: bad recursion, still works

2008-07-16 Thread oj
On Jul 16, 1:09 pm, Jeff <[EMAIL PROTECTED]> wrote: > On Jul 15, 7:21 pm, Michael Torrie <[EMAIL PROTECTED]> wrote: > > > iu2 wrote: > > > I still don't understand: In each recursive call to flatten, acc > > > should be bound to a new [], shouldn't it? Why does the binding happen > > > only on the

Re: bad recursion, still works

2008-07-16 Thread Jeff
On Jul 15, 7:21 pm, Michael Torrie <[EMAIL PROTECTED]> wrote: > iu2 wrote: > > I still don't understand: In each recursive call to flatten, acc > > should be bound to a new [], shouldn't it? Why does the binding happen > > only on the first call to flatten? > > Nope.  In each new call it's (re)boun

Re: bad recursion, still works

2008-07-16 Thread iu2
On Jul 16, 2:21 am, Michael Torrie <[EMAIL PROTECTED]> wrote: > iu2 wrote: > > I still don't understand: In each recursive call to flatten, acc > > should be bound to a new [], shouldn't it? Why does the binding happen > > only on the first call to flatten? > > Nope.  In each new call it's (re)boun

Re: bad recursion, still works

2008-07-15 Thread Michael Torrie
iu2 wrote: > I still don't understand: In each recursive call to flatten, acc > should be bound to a new [], shouldn't it? Why does the binding happen > only on the first call to flatten? Nope. In each new call it's (re)bound to the same original list, which you've added to as your function conti

Re: bad recursion, still works

2008-07-15 Thread mdsherry
On Jul 15, 4:12 pm, iu2 <[EMAIL PROTECTED]> wrote: > On Jul 15, 9:30 pm, [EMAIL PROTECTED] wrote: > > > > > On Jul 15, 2:59 pm, iu2 <[EMAIL PROTECTED]> wrote: > > > > Hi, > > > > I wrote this wrong recursive function that flattens a list: > > > > def flatten(lst, acc=[]): > > >     #print 'acc =',

Re: bad recursion, still works

2008-07-15 Thread iu2
On Jul 15, 9:30 pm, [EMAIL PROTECTED] wrote: > On Jul 15, 2:59 pm, iu2 <[EMAIL PROTECTED]> wrote: > > > > > Hi, > > > I wrote this wrong recursive function that flattens a list: > > > def flatten(lst, acc=[]): > >     #print 'acc =', acc, 'lst =', lst > >     if type(lst) != list: > >         acc.a

Re: bad recursion, still works

2008-07-15 Thread mdsherry
On Jul 15, 2:59 pm, iu2 <[EMAIL PROTECTED]> wrote: > Hi, > > I wrote this wrong recursive function that flattens a list: > > def flatten(lst, acc=[]): >     #print 'acc =', acc, 'lst =', lst >     if type(lst) != list: >         acc.append(lst) >     else: >         for item in lst: >             f

bad recursion, still works

2008-07-15 Thread iu2
Hi, I wrote this wrong recursive function that flattens a list: def flatten(lst, acc=[]): #print 'acc =', acc, 'lst =', lst if type(lst) != list: acc.append(lst) else: for item in lst: flatten(item) return acc a = [1, 2, [3, 4, 5], [6, [7, 8, [9, 10],