[Tutor] problems with the bisect method

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Thu Mar 6 06:27:01 2003


Hello Mic,

Wednesday, March 5, 2003, 6:16:00 AM, you wrote:

[snip]

>>>> def fun(k, s):
MF>         return f(k, s) - j

>>>> delta = 1e-9
>>>> bisect(delta, 1-delta, delta, fun)
MF> Traceback (most recent call last):
MF>   File "<pyshell#10>", line 1, in ?
MF>     bisect(delta, 1-delta, delta, fun)
MF>   File "<pyshell#5>", line 2, in bisect
MF>     fMax = function(max)
MF> TypeError: fun() takes exactly 2 arguments (1 given)
>>>> 

Quite reasonable I must say :). Your function fun takes couple of
arguments---k and s and f too althouh s argument seems to be unused.

You may modify it as:

def f(x):
    return ...

def fun(k):
    return f(k) - j

Or write a wrapper function:

def wrapFun(x):
    return fun(x, some constant goes here)

Or even with lambda:

bisect(delta, 1 - delta, delta, lambda x: fun(x, smth...))

BTW, although you script is quite short, you'd better avoid global
variables like j.

If you need a set of functions like fun. Python can provide with
better (IMHO) options:

1. def makeFun(j):
       def newF(x):
           f(x) - j
       return newF

2. Lambdas again: myF = lambda x: f(x) - j

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru