In article [EMAIL PROTECTED],
Matteo wrote:
This last approach could, at least theoretically, create an arbitrarily
long list of polys, without overflowing any kind of stack. In practice,
python does not seem to perform tail recursion optimizations, and conks
out after makepolys(997) on my
Pekka Karjalainen [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]
Python doesn't do any kind of tail recursion optimization by default
because Guido has decided it shouldn't (at least so far). I'm sure it has
been discussed in detail in Python development forums lists, but I
Josiah Manson wrote:
In the following program I am trying to learn how to use functional
programming aspects of python, but the following program will crash,
claiming that the recursion depth is too great. I am attempting to make
a list of polynomial functions such that poly[0](3) = 1, poly[1
In [EMAIL PROTECTED], Tim Chase
wrote:
My understanding is that the lambda-defined functions are not
called until the actual application thereof, with the
mypolys = make_polys(8)
mypolys[5](2) #the lambda call happens here, no?
Yes, that's right.
/F's original statement read
In the following program I am trying to learn how to use functional
programming aspects of python, but the following program will crash,
claiming that the recursion depth is too great. I am attempting to make
a list of polynomial functions such that poly[0](3) = 1, poly[1](3) =
3, poly[2](3) = 9
In [EMAIL PROTECTED], Josiah Manson
wrote:
def make_polys(n):
Make a list of polynomial functions up to order n.
p = lambda x: 1
polys = [p]
for i in range(n):
polys.append(lambda x: polys[i](x)*x)
The `i` is the problem. It's not evaluated
The `i` is the problem. It's not evaluated when the lambda
*definition* is executed but when the lambda function is
called. And then `i` is always == `n`. You have to
explicitly bind it as default value in the lambda definition:
polys.append(lambda x, i=i: polys[i](x)*x)
Then it
Tim Chase wrote:
The `i` is the problem. It's not evaluated when the lambda
*definition* is executed but when the lambda function is
called. And then `i` is always == `n`. You have to
explicitly bind it as default value in the lambda definition:
polys.append(lambda x, i=i:
will
have the same value for all lambdas.
There's some subtle behavior here that I'm missing.
lexical closures bind to names, default arguments bind to values.
Just to be a bit more explicit:
In code like:
def make_polys(n):
Make a list of polynomial functions up to order n
The `i` is the problem. It's not evaluated when the lambda *definition*
is executed but when the lambda function is called. And then `i` is
always == `n`. You have to explicitly bind it as default value in the
lambda definition:
polys.append(lambda x, i=i: polys[i](x)*x)
Just to be a bit more explicit:
In code like:
def make_polys(n):
Make a list of polynomial functions up to order n.
p = lambda x: 1
polys = [p]
for i in range(n):
polys.append(lambda x: polys[i](x)*x)
i=3
The lambda-defined
I'm curious why the very first attempt to call p(3) doesn't bomb
out with the NameError that polys wasn't defined before it even
got to the point of attempting to call it.
In the first call, the 0th lambda function is evaluated, and it was
defined as the constant function 1. The functions
12 matches
Mail list logo