P

2008/12/16  <rdmur...@bitdance.com>:
> Quoth "=?ISO-8859-1?Q?Juan_Pablo_Romero_M=E9ndez?=" <jpablo.rom...@gmail.com>:
>> Hello,
>>
>> Suppose this function is given:
>>
>> def f(x,y):
>>   return x+y+k
>>
>> Is it possible to somehow assign a value to k without resorting to
>> making k global?
>>
>> I'm thinking something like this:
>>
>> eval("f(1,1)", {"f":f, "k":1})
>>
>> Or even better, something like:
>>
>> def g(k):
>>   return f
>>
>> g(1)(1,1) ==> 3
>
>    >>> def g(k):
>    ...     def f(x,y):
>    ...             return x+y+k
>    ...     return f
>    ...
>    >>> g(1)(1,1)
>    3
>
> But what's the use case?  The above might not satisfy it.


I'm making a 3D plotting library (yet another!), but with facilities
to graph "parametrized" surfaces. The current code is used like this:

...
m = Mesh((-1,1),(-1,1))
m.addQuad( lambda v,w, x, y: (x, y, - w * x**2 - v * y**2 ) )
m.addParameter(('v',0,1))
m.addParameter(('w',0,1))
...


Which plots the surface using OpenInventor and add sliders for each
parameter. I'm using QTimeLine, so the slider can be animated, and on
each step the mesh is recalculated, thus deforming the mesh on real
time.

What I'd like to do is something like this:
...
m = Mesh((-1,1),(-1,1))
m.addQuad( lambda x, y: (x, y, - w * x**2 - v * y**2 ) )
...

and have the code automatically figure out that w and v are free
variables and generate the right code.

Right now I can catch w and v (using NameError), but can't figure out
how to get python to assign values to w and v (other than declaring
them globals).

My code needs a function g(x,y) which does not depend on w and v.
Given this function:

func = lambda v,w, x, y: (x, y, - w * x**2 - v * y**2 )

I use functools.partial, as:

g = partial(func,1,1)

and then use g to actually generate the mesh for each value of v and w.


   Juan Pablo


>
> --RDM
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to