self-confidence,the source of all the power

导航

python string to variable

example:

exec:执行字串语句

>>> l=[12,3,4,4]
>>> s = 'l.append(10)'
>>> exec(s)
>>> s
'l.append(10)'
>>> l
[12, 3, 4, 4, 10]
>>>

 

eval:让字串变成与之对应的变量名

>>> str = 'good'
>>> good = 'hello!world'
>>> eval(str)
'hello!world'
>>> good
'hello!world'

 

 

posted on 2012-11-05 17:10  漩涡鸣人  阅读(344)  评论(0编辑  收藏  举报