使用eval或类似的东西...... [英] Using eval, or something like it...
问题描述
您好,
我知道您可以使用eval动态生成您可能想要调用的函数名称
。是否(或某些等效方法)也可以用来为一个类的变量做同样的事情,例如
class Foo():
bar = 1
gum = 2
mylist = [''bar'',''gum'']
a = Foo()
每个mylist
:
a.eval(每个)= 999
如果是这样,那么正确的语法/方法是什么。
问候,
Roger。
Hi There,
I know you can use eval to dynamically generate the name of a function
you may want to call. Can it (or some equivalent method) also be used to
do the same thing for the variables of a class e.g.
class Foo():
bar = 1
gum = 2
mylist = [''bar'',''gum'']
a = Foo()
for each in mylist:
a.eval(each) = 999
If so, what is the proper syntax/method for this.
Regards,
Roger.
推荐答案
不要使用eval!
2008年11月20日星期四上午10:44,r0g< ai ** **** @ techicalbloke.comwrote:
DON''T USE eval!
On Thu, Nov 20, 2008 at 10:44 AM, r0g <ai******@technicalbloke.comwrote:
你好,
我知道你可以使用eval来动态生成功能名称
你可能想打电话。是否(或某些等效方法)也可以用来为一个类的变量做同样的事情,例如
class Foo():
bar = 1
gum = 2
mylist = [''bar'',''gum'']
a = Foo()
每个mylist
:
a.eval(每个)= 999
如果是这样,那么正确的语法/方法是什么。
问候,
Roger。
-
http://mail.python .org / mailman / listinfo / python-list
-
-
- 问题通过方法解决
--
--
-- "Problems are solved by method"
11月19日,7:44 * pm,r0g< aioe .... @ technicalbloke。编写:
On Nov 19, 7:44*pm, r0g <aioe....@technicalbloke.comwrote:
你好,
我知道你可以使用eval动态生成函数的名称
你可能想要打电话。是否(或某些等效方法)也可以用来为一个类的变量做同样的事情,例如
class Foo():
* bar = 1
* gum = 2
mylist = [''bar'',''gum'']
a = Foo()
每个mylist
:
* a.eval(每个)= 999
如果是这样,那么正确的语法/方法是什么。每个在mylist中
Hi There,
I know you can use eval to dynamically generate the name of a function
you may want to call. Can it (or some equivalent method) also be used to
do the same thing for the variables of a class e.g.
class Foo():
* bar = 1
* gum = 2
mylist = [''bar'',''gum'']
a = Foo()
for each in mylist:
* a.eval(each) = 999
If so, what is the proper syntax/method for this.
:
setattr(a,each,999)
HTH,>
George
for each in mylist:
setattr(a, each, 999)
HTH,
George
11月20日上午11:44 *上午,r0g< aioe .... @ technicalbloke.comwrote:
On Nov 20, 11:44*am, r0g <aioe....@technicalbloke.comwrote:
你好,
我知道你可以用eval动态生成一个函数的名字
你可能想要的打电话。是否(或某些等效方法)也可以用来为一个类的变量做同样的事情,例如
class Foo():
* bar = 1
* gum = 2
mylist = [''bar'',''gum'']
a = Foo()
每个mylist
:
* a.eval(每个)= 999
如果是这样,那么正确的语法/方法是什么。
Hi There,
I know you can use eval to dynamically generate the name of a function
you may want to call. Can it (or some equivalent method) also be used to
do the same thing for the variables of a class e.g.
class Foo():
* bar = 1
* gum = 2
mylist = [''bar'',''gum'']
a = Foo()
for each in mylist:
* a.eval(each) = 999
If so, what is the proper syntax/method for this.
你提到一个类的变量但是你继续戳一下这个班级的
实例。他们是两个不同的东西。你是哪一个?b $ b是什么意思?
无论如何,使用内置函数setattr来设置属性值
对象或类。
setattr(a,''bar'',999)相当于a.bar = 999
setattr(Foo, ''bar'',456)相当于Foo.bar = 456
查看文档中的setattr(和getattr)。
You mention "variables of a class" but you then proceed to poke at an
instance of the class. They are two different things. Which do you
mean?
In any case, use the built-in function setattr to set attribute values
for an object or for a class.
setattr(a, ''bar'', 999) is equivalent to a.bar = 999
setattr(Foo, ''bar'', 456) is equivalent to Foo.bar = 456
Check out setattr (and getattr) in the docs.
>
这篇关于使用eval或类似的东西......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!