mysql类似eval的函数,使用eval或类似的东西......

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.

解决方案DON''T USE eval!

On Thu, Nov 20, 2008 at 10:44 AM, r0g

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.

--

http://mail.python.org/mailman/listinfo/python-list

--

--

-- "Problems are solved by method"

On Nov 19, 7:44*pm, r0g

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.

for each in mylist:

setattr(a, each, 999)

HTH,

George

On Nov 20, 11:44*am, r0g

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值