list of polynomial functions

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Thu Jun 15 14:02:02 EDT 2006


In <1150390140.459461.169900 at i40g2000cwc.googlegroups.com>, 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 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 works.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list