我该怎么做呢? (左手边的eval()) [英] How do I do this? (eval() on the left hand side)
问题描述
我是Python语言的新手。
如何做这样的事情:
我知道
a = 3
y =" a"
print eval(y)
会给我打印出3 - 但是我该如何做以下的效果:
eval(y)= 4#希望a的值变为4
??
谢谢,
-
这是'我
I am new to the Python language.
How do I do something like this:
I know that
a = 3
y = "a"
print eval(y)
would give me a print out of 3 - but how do I do something to the effect of:
eval(y) = 4 # hopefully the value of a gets changed to 4
??
Thanks,
--
It''s me
推荐答案
这是我
a = 3 > y =" a"
print eval(y)
a = 3
y = "a"
print eval(y)
要在这里得到''a'',你会说
a = 4
我不确定你为什么要这样做?也许你可以用b $ b概述一下你想做什么?这将有助于
。你的目标是使用简单的
数据类型进行指针仿真吗?
谢谢
Caleb
To get ''a'' to be 4 here, you would say
a = 4
I am not sure why you would want to do otherwise? Perhaps you could
sketch out a little more about what you are trying to do? That would help
a lot. Are you aiming for something like pointer emulation with simple
datatypes?
Thanks
Caleb
" Caleb Hattingh" < CA **** @ telkomsa.net>在消息中写道
news:op ************** @ news.telkomsa.net ...
"Caleb Hattingh" <ca****@telkomsa.net> wrote in message
news:op**************@news.telkomsa.net...
这是我
a = 3
y =" a"
print eval(y)
要获得' 'a''在这里4,你会说
a = 4
a = 3
y = "a"
print eval(y)
To get ''a'' to be 4 here, you would say
a = 4
显然但这不是我想要的做。
我不确定你为什么要这样做?也许你可以更多地了解你想做什么?这将有很大帮助。你的目标是使用简单的数据类型的指针模拟吗?
例如,在REXX中,可以做一个:
解释y''= 4''
因为y包含a,所以上述语句中包括:
a = 4 >
有很多情况下这很有用。例如,你可能会获得一个输入,这是一个表示变量名称的字符串,并且你想要评估表达式的
(就像一个计算器应用程序,用于
实例)。
谢谢
Caleb
Obviously but that''s not what I wish to do.
I am not sure why you would want to do otherwise? Perhaps you could
sketch out a little more about what you are trying to do? That would help
a lot. Are you aiming for something like pointer emulation with simple
datatypes?
In REXX, for instance, one can do a:
interpret y'' = 4''
Since y contains a, then the above statement amongs to:
a = 4
There are many situations where this is useful. For instance, you might be
getting an input which is a string representing the name of a variable and
you wish to evaluate the expression (like a calculator application, for
instance).
Thanks
Caleb
当然,好的,我想我现在和你在一起。
你得到一个(例如)变量名作为字符串,你知道怎么评价
用和 ; eval",但你也希望能够分配回(通过)
字符串表示?
单向(如果我理解正确的话) )是全局或本地人
dicts。在IDLE中试试这个:
''>>> a = 3
''>>> y =''''
''>>>评估(y)
''>>> d = locals()#获取本地变量字典
''>>> d [''a'']
''>>> d [y]
''>>> d [y] = 8#y是一个字符串=''a''
''>>> a#更改了a的值。
8
''>>>
这有点什么你的意思是?我还是新手(并且不知道来自Adam的REXX
)。
谢谢
Caleb
Sure, ok, I think I am with you now.
You get a (e.g.) variable name as a string, and you KNOW how to evaluate
it with "eval", but you also want to be able to assign back to (through)
the string representation?
One way (if I understand you correctly) is with the globals or locals
dicts. Try this in IDLE:
''>>> a = 3
''>>> y = ''a''
''>>> eval(y)
3
''>>> d = locals() # Get a dictionary of local variables
''>>> d[''a'']
3
''>>> d[y]
3
''>>> d[y] = 8 # y is a string = ''a''
''>>> a # The value of a is changed.
8
''>>>
Is this kinda what you mean? I''m still new at this (and don''t know REXX
from Adam).
Thanks
Caleb
有很多情况下这很有用。例如,您可能正在获取一个输入,这是一个表示变量名称的字符串
和
您希望评估表达式(如计算器应用程序,用于
实例)。
There are many situations where this is useful. For instance, you
might be
getting an input which is a string representing the name of a variable
and
you wish to evaluate the expression (like a calculator application, for
instance).
这篇关于我该怎么做呢? (左手边的eval())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!