[Edu-sig] More Pythonic Precalculus (Python 2.0)

Kirby Urner pdx4d@teleport.com
Mon, 25 Sep 2000 19:18:52 -0700


At 01:15 PM 09/25/2000 -0500, Dustin Mitchell wrote:
>Kirby --
>
>How about
>
>>>> f = lambda x : 2 * x * x - 10 * x + 2

Dustin, re your lambda-based approach, Jeff Cogswell just sent 
me the following:

>>> f = "2 * x**2 - 10 * x + 2"
>>> foo = eval("lambda x: %s" % f)
>>> for i in range(10): foo(i)

2
-6
-10
-10
-6
2
14
30
50
74

...which I think is brilliant, and likely faster than even 
David's solution since it evals only once.  Here lambda makes 
more sense, and we get to start with a string.

Kirby

PS:  there's likely a solution with compile and/or exec as 
well, but I haven't thought it through.