Re: free variables /cell objects question

2007-01-25 Thread gangesmaster
[Steven] > My solution is, don't try to have one function do too much. Making a list > of foos should be a separate operation from making a single foo: that's exactly what my second example does (as well as my production code) [Paul] > But it does work as expected, if your expectations are based

Re: free variables /cell objects question

2007-01-25 Thread Steven D'Aprano
On Thu, 25 Jan 2007 04:29:35 -0800, Paul Rubin wrote: > "gangesmaster" <[EMAIL PROTECTED]> writes: >> what i see as a bug is this code not working as expected: >> >> >>> def make_foos(names): >> ... funcs = [] >> ... for n in names: >> ... def foo(): >> ...

Re: free variables /cell objects question

2007-01-25 Thread Paul Rubin
"gangesmaster" <[EMAIL PROTECTED]> writes: > what i see as a bug is this code not working as expected: > > >>> def make_foos(names): > ... funcs = [] > ... for n in names: > ... def foo(): > ... print "my name is", n > ... funcs.append(foo) > ...

Re: free variables /cell objects question

2007-01-25 Thread gangesmaster
no, it has nothing to do with "i" being global. >>> tuple(lambda: i for i in range(10))[0]() 9 >>> tuple(lambda: i for i in range(10))[1]() 9 what i see as a bug is this code not working as expected: >>> def make_foos(names): ... funcs = [] ... for n in names: ... def foo():

Re: free variables /cell objects question

2007-01-23 Thread Carl Banks
On Jan 23, 9:46 am, "gangesmaster" <[EMAIL PROTECTED]> wrote: > ugliness :) > > so this is why [lambda: i for i in range(10)] will always return 9. > imho that's a bug, not a feature. It's a feature. If you were to write a nested function like this, where a, b, c, and d, are cell variables: prin

Re: free variables /cell objects question

2007-01-23 Thread Terry Reedy
"Paul Rubin" <"http://phr.cx"@NOSPAM.invalid> wrote in message news:[EMAIL PROTECTED] | or the Python idiom [(lambda i=i: i) for i in range(10)] While the idiom is commonly written this way, I think it would be clearer if the two related but different variables had two different names: [(lam

Re: free variables /cell objects question

2007-01-23 Thread Terry Reedy
"gangesmaster" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] | so this is why [lambda: i for i in range(10)] will always return 9. No, it returns a list of 10 identical functions which each return the current (when executed) global (module) variable i. Except for names, 'lambda:i

Re: free variables /cell objects question

2007-01-23 Thread Duncan Booth
"gangesmaster" <[EMAIL PROTECTED]> wrote: > ugliness :) > > so this is why [lambda: i for i in range(10)] will always return 9. > imho that's a bug, not a feature. Horses for courses. There are cases where you need to get the latest value of the bound variable, and there are cases where you wa

Re: free variables /cell objects question

2007-01-23 Thread Paul Rubin
"gangesmaster" <[EMAIL PROTECTED]> writes: > so this is why [lambda: i for i in range(10)] will always return 9. > imho that's a bug, not a feature. Use [(lambda j: lambda: j)(i) for i in range(10)] or the Python idiom [(lambda i=i: i) for i in range(10)] -- http://mail.python.org/mailman/listinf

Re: free variables /cell objects question

2007-01-23 Thread gangesmaster
ugliness :) so this is why [lambda: i for i in range(10)] will always return 9. imho that's a bug, not a feature. thanks. -tomer Duncan Booth wrote: > "gangesmaster" <[EMAIL PROTECTED]> wrote: > > > what problem does the cell object solve? > > The closure represents the variable, not the object

Re: free variables /cell objects question

2007-01-23 Thread Duncan Booth
"gangesmaster" <[EMAIL PROTECTED]> wrote: > what problem does the cell object solve? The closure represents the variable, not the object. So if x is rebound to a different object your inner function g() will now access the new object. If the object itself was passed to MAKE_CLOSURE then g woul

Re: free variables /cell objects question

2007-01-23 Thread Peter Otten
gangesmaster wrote: > why does CPython require to wrap the free variables if > closure functions by a cell objects? > why can't it just pass the object itself? > def f(x): > ... def g(): > ... return x+2 > ... return g > ... g5 = f(5) dis(g5) > 3 0 L

free variables /cell objects question

2007-01-23 Thread gangesmaster
why does CPython require to wrap the free variables if closure functions by a cell objects? why can't it just pass the object itself? >>> def f(x): ... def g(): ... return x+2 ... return g ... >>> g5 = f(5) >>> dis(g5) 3 0 LOAD_DEREF 0 (x)