0

How can I pass variables to an eval function?

I have tried some solutions posted here on SO as well as reading through the Python documentation

I have this section of code 'v' which I would like to reuse in another part of my code. Here is a snippet:

 v = lambda r5, cdate : somefunc(r4, cdate) + math.exp(-t_3m*r1 - t_6m*r2 - t_1y*r3 - t_2y*r4 - retfrac(d[4], d[5], cdate, eoffset = 730)*r5) + \
                                                      math.exp(-t_3m*r1 - t_6m*r2 - t_1y*r3 - t_2y*r4 - t_3y*r5)

I would then like to do something to the likes of:

w = "v(r5, cdate) + math.exp(-t_3m*r1 - t_6m*r2 - t_1y*r3 - t_2y*r4 - t_3y*r5)*(0"
# + some other expressions that I have to build up using a for loop since there is no closed form solution
func = lambda r6, cdate = i[30] : (five_year + (coupon_five_year/2)*(1-(ia_5y/.5))) - (coupon_five_year/2)*(eval(w)) - 100*math.exp(-t_3m*r1 - t_6m*r2 - t_1y*r3 - t_2y*r4 - t_3y*r5 - t_5y*r6)
                r6 = fsolve(func, xguess)[0]

When I try to evaluate this, I am getting an error that says:

NameError: name 'v' is not defined

Once I remove v, from the expression for w, I then get a NameError for t_3m and it just waters through all of the variables. Could someone please help me out?

2
  • 1
    v is a function, why can't you pass arguments to it directly? Commented Jun 11, 2019 at 17:28
  • I have tried moving it directly to the function statement, but then I still get a name error on w. I also get an index out of bounds error on 'd' I believe, but I have verified that d has nearly 20 elements. Commented Jun 11, 2019 at 17:31

1 Answer 1

1

The eval function accepts two additional parameters to provide it with global and local variables.

You simply need to call it like this: eval(w,globals(),locals())

That's assuming the variable references made in the w string are all in scope when you call eval().

Sign up to request clarification or add additional context in comments.

7 Comments

So I did this, and it sorta seems to work. For some reason, it raises an index error for an array that I reference in the string w (the string that gets evaluated. Is it not passing this array properly? I have verified that the array has 20 elements while I call on elements 5 and 6.
It is sometimes hard to debug issues with eval(). You might try to temporarily replace your eval() call with the actual code contained in the string so that Python can hopefully return a more meaningful error. You could also try to print locals()["variableName"] before the call to see what the function is actually working with.
Ah, well the list I am using is called d, and there is another list in another program called d which only has 4 elements - hence, causing the problem I believe, The list d in the local scope is correct while the list d in the global scope is bad. Is there anyway I can do, eval(w, None, locals()) and have it work?
Yes it would work but then your formula may not be able to use some of your global functions, What you may be able to do is to create a merged local dictionary to pass instead of locals(). e.g. mergedDict = {**globals(),**locals()} and then call eval(w,None,mergedDict). This will let you control the precedence of variable names (although I'm surprised that eval() would use a global 'd' when there is a local 'd' available).
Finally got that part to work by declaring d as a global to start with. One more quick question: I am getting error that says python TypeError: <lambda>() takes 1 positional argument but 2 were given I am sending func r6, and cdate = i[30] which is then used in the evaluation of w, where v(r5, cdate) sits. Do you know the source of this error?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.