4 ms·
Yeah, i gave up on doing fp (or any mathematics) in python when I got surprised by this: >>> monomials = [lambda x: x(star)(star)i for i in [1,2,3]] #(not sure
by sixbrx 13y ago
Yeah, i gave up on doing fp (or any mathematics) in python when I got surprised by this:
>>> monomials = [lambda x: x(star)(star)i for i in [1,2,3]] #(not sure how to escape stars for exponentiation properly, whatever)
Those all end up being the same monomial because the integer i is captured by address and not given a new binding for every iteration. Very surprising, and an accident waiting to happen for anyone used to dealing with integers by value (hard to say that with a straight face).
The hackish workaround is "lambda x,i=i: x(star)(star)i" but I'd forget it all the time.
- tome 13y agoWoah, I was aware of the name capture problem in closures, but I hadn't realised this was one of the consequences. That's very unfortunate.
- zanny 13y agoThis is just like how python default values are global variables that if you modify over time (ie, def foo(x = []): x.append(0), the second iteration x would be [0,0], because it is only initialized once.