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

Jeff Cogswell jeff@cogswell.net
Tue, 26 Sep 2000 09:29:11 EDT


Thanks for sharing my tip on the list, Kirby.

I think I have things fixed and this email *should* make it to the list now.

Jeff

>From: Kirby Urner <pdx4d@teleport.com>
>To: Edu-sig@python.org
>Subject: Re: [Edu-sig] More Pythonic Precalculus (Python 2.0)
>Date: 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.
>
>
>
>_______________________________________________
>Edu-sig mailing list
>Edu-sig@python.org
>http://www.python.org/mailman/listinfo/edu-sig

_________________________________________________________________________
Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com.

Share information about yourself, create your own public profile at 
http://profiles.msn.com.