On Jan 30, 2008 10:10 AM, Charles R Harris <[EMAIL PROTECTED]>
wrote:

>
>
> On Jan 30, 2008 2:22 AM, Gael Varoquaux <[EMAIL PROTECTED]>
> wrote:
>
> > On Wed, Jan 30, 2008 at 12:49:44AM -0800, LB wrote:
> > > My problem is that the complexe calculations made in calc_0d use some
> > > parameters, which are currently defined at the head of my python file.
> > > This is not very nice and I can't define a module containing theses
> > > two functions and call them with different parameters.
> >
> > > I would like to make this cleaner and pass theses parameter as
> > > keyword  argument, but this don't seems to be possible with vectorize.
> > > Indeed, some of theses parameters are array parameters and only the x
> > > and y arguments should be interpreted with the broadcasting rules....
> >
> > > What is the "good way" for doing this ?
> >
> > I don't know what the "good way" is, but you can always use functional
> > programming style (Oh, no, CaML is getting on me !):
> >
> > def calc_0d_params(param1, param2, param3):
> >    def calc_0d(x, y):
> >        # Here your code making use of param1, param2, param3)
> >        ...
> >
> >    return calc_0d(x, y)
> >
> > you call the function like this:
> >
> > calc_0d_params(param1, param2, param3)(x, y)
> >
> > To vectorize it you can do:
> >
> > calc_0d_vect = lambda *params: vectorize(calc_0d_params(*params))
> >
> > This is untested code, but I hope you get the idea. It all about partial
> > evaluation of arguments. By the way, the parameters can now be keyword
> > arguments.
> >
>
> IIRC, the way to do closures in Python is something like
>
> In [5]: def factory(x) :
>    ...:     def f() :
>    ...:         print x
>    ...:     f.x = x
>    ...:     return f
>    ...:
>

Oops, looks like that needs to be:

In [5]: def factory(x) :
   ...:     def f() :
   ...:         print f.x
   ...:     f.x = x
   ...:     return f
   ...:

You can also do something simpler:

In [51]: def f() : print f.x
   ....:

In [52]: f.x = "Hello World!"

In [53]: f()
Hello World!

Chuck
_______________________________________________
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

Reply via email to