Peter Otten <__pete...@web.de> Wrote in message:
> André Walker-Loud <walksl...@gmail.com> wrote:
> 
>> Hello python tutors,
>> 
>> I am utilizing a 3rd party numerical minimization routine.  This routine
>> requires an input function, which takes as arguments, only the variables
>> with which to solve for.  But I don’t want to define all possible input
>> functions, in a giant switch, but rather, if I know I am fitting a
>> polynomial, I would like to just pass a list of parameters and have the
>> code know how to construct this function.
>> 
>> To construct for example, a chisq function, you must pass not only the
>> variables to solve for, but also the data, uncertainties, and perhaps
>> other arguments.  So it requires a little hacking to get it to work.  With
>> the help of my friends and looking at similar code, I have come up with
>> two ways that work under my simple test cases, and I have a few questions
>> about them.
>> 
>> The 3rd party minimizer utilizes the .func_code.co_varnames and
>> .func_code.co_argcount to determine the name and number of variables to
>> minimize.  eg.
>> 
>>> g = lambda x,c_0,c_1: c_0 + c_1 * x
>>> g.func_code.co_varnames
>> ('x', 'c_0', 'c_1’)
>>> g.func_code.co_argcount
>> 3
>> 
>> so what is needed is a function
>>> def f(c_0,c_1):
>>> …#construct chi_sq(c_0,c_1,x,y,…)
>> 
>> 
>> 
>> Question 1:
>> Is there a better way to accomplish (my hopefully clear) goals?
> 
> I think you are looking for closures:
> 
> def make_poly(coeff):

> 

I would also recommend closures,  but the particular case seems to
 fit partial pretty well. 


 http://docs.python.org/2/library/functools.html#functools.partial


-- 
DaveA

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to