eval's local arguement ignored?

Jean-S?bastien Bolduc jseb at cs.mcgill.ca
Tue Sep 30 09:36:44 EDT 2003


> **kwa** is the solution!
> 
> In your factory-function *foo* you have to declare *a*
> keywordarguement which is initialized with a.
> 
> >>> def foo():
> ... 	a=1
> ... 	f=lambda x,a=a:a*x
> ... 	return f

Not exactly what I'm looking for, I'm afraid. A more complete picture
is this: I'm writing a class such as this:

class Foo:
  def __init__(self, fnc, **params):
    ...
  def evaluate(self, val)
    ...

That has to be instantiated as, e.g.:

x = Foo( 'lambda x : a*x', dict( a = 2. ) )

so that "x.evaluate( 5. )" will return, in this case, "2.*5.".

The approach you describe will certainly work, but the thing is that
this class will have to be used by people who don't necessarily know
Python (yet). So I would really like the lambda function's parameter
list to always be the same, whatever you put on its RHS.

Once again, I don't understand why the "eval", as described above with
"globals" and "locals" arguments specified, will not work. Is there a
way to modify a function's closure?

Thanks,
JSeb




More information about the Python-list mailing list