[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
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():
>> ...
"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)
> ...
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():
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
"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
"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
"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
"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
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
"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
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
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)
13 matches
Mail list logo