[Edu-sig] Algebra + Python

Kirby Urner pdx4d@teleport.com
Mon, 30 Apr 2001 22:05:00 -0700



>Kirby wrote:
>> There may be a better way to write the factory function than
>> I've shown below.  I'd like to see other solutions:
>> 
>>  >>> def makepoly(A,B,C):     
>>         """
>>         Build a polynomial function from coefficients
>>         """
>>         return eval("lambda x: %s*x**2 + %s*x + %s" % (A,B,C))
>
>How about this?
>
> class poly:
>    def __init__(self, A, B, C):
>        self.A, self.B, self.C = A, B, C
>    def __call__(self, x):
>        return self.A * x**2 + self.B * x + self.C
>
> >>> f = poly(2,3,4)
> >>> f(10)
> 234

I like it!

Kirby