0

I tried to assign some value to output of the eval function as below:

d = {"a": 10}
st1 = 'd'
st2 = '["a"]'
eval(st1 + st2) = 15

I got this error:

File "<stdin>", line 1
SyntaxError: can't assign to function call

I also tried this:

x = eval(st1 + st2)
x = 15

But it doesn't change the d dictionary. I tried to eval the whole assignment like this:

eval(st1 + st2 + ' = 15')

But I got this error:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<string>", line 1
  d["a"] = 15
         ^
SyntaxError: invalid syntax

I also have tried to use ast.literal_eval() function and all the results were the same. So, any idea how to do it??

EDIT 1:

For clarification as @LennartRegebro requested for, I should say that I have some dictionaries with some specific key and value pairs. I have a text file which I get from user and some of these values are defined there and I should change basic dictionaries values to these user defined ones. I have parser which parses the text file and for each change, it gives me a tuple containing dictionary name, key and value; all are strings. I wanted to do the assignments by eval function, which I understood that I can't.

EDIT 2:

As @lejlot suggested, I used globals() and added below lines to my code:

import re
pattern = re.compile(r'\["([A-Za-z0-9_\./\\-]+)"\]')
globals()[st1][pattern.findall(st2)[0]] = 15
Zeinab Abbasimazar
  • 9,835
  • 23
  • 82
  • 131

2 Answers2

3

Why don't you access your variables through globals() (or locals()) instead of eval?

d={'a':10}
globals()['d']['a']=15
print d['a']

in your particular case

d={'a':10}
s1='d'
s2='a'
globals()[s1][s2]=15
print d['a']
lejlot
  • 64,777
  • 8
  • 131
  • 164
0

Do a simple,

exec(st1 + st2 + '= 15')

eval won't work. Use exec instead. That too assignment should be done inside within the argument itself.

Though using exec is a highly unrecommended practice.

Sushant Gupta
  • 8,980
  • 5
  • 43
  • 48
  • 1
    exec() is (almost) never the correct solution and certainly not the correct solution to a newbie SO question. It *works* yes, but it's not the correct way to solve the problem. – Lennart Regebro Apr 07 '14 at 05:08
  • Yeah, but even what he is trying to achieve is unclear. And to what he desired, I thought to give this hack. I know this is a horrendous practice. – Sushant Gupta Apr 07 '14 at 05:09
  • The you should at least say so in the answer. – Lennart Regebro Apr 07 '14 at 05:10
  • @LennartRegebro, Sushant's answer worked for me, but what is better than this so? Can you give me an alternative? – Zeinab Abbasimazar Apr 07 '14 at 05:53
  • I do agree, that this should not be considered as the "solution". exec has its own use, but not in such simple tasks, which can be solved in more simple, safe way. – lejlot Apr 07 '14 at 06:01
  • 1
    @ZeinabAbbasi: Anything is better than this. I can't give you an alternative as you have not explained the problem. (OK, you explained it in an edit above, I see now). Lejlot's solution is better. – Lennart Regebro Apr 07 '14 at 06:02
  • @ZeinabAbbasi Sorry, I couldn't figure out your gender from your name. Iranian names are new to me :) – Sushant Gupta Apr 07 '14 at 06:16
  • @SushantGupta, I'm Iranian, but my name is actually arabic. – Zeinab Abbasimazar Apr 07 '14 at 07:07