python用str作为变量名,Re:使用str作为变量名

2008/9/4 Chris Rebert :On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot

>Hi,

I have a program that take a word as argument, and I would like to

link this word to a class variable.

eg.

class foo():

You should subclass ''object'', so that should be:

class Foo(object):

> width = 10

height = 20

a=foo()

arg=''height''

a.__argname__= new_value

You''re looking for the setattr() built-in function. In this exact case:

setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,

Chris

Indeed.

I''ll use:

a.__setattr__(height, new_value)

Thanks to all

Mathieu

解决方案Mathieu Prevot a écrit :

2008/9/4 Chris Rebert :

(snip)

>You''re looking for the setattr() built-in function. In this exact case:

setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,

Chris

Indeed.

I''ll use:

a.__setattr__(height, new_value)

Please don''t. Use the generic setattr() function instead. This holds for

any __magic__ method : they are *implementation* for operators and

generic functions - which you can think of as operators with a function

syntax -, and are not meant to be called directly. You wouldn''t write

something like 2.__add__(3), would you ?

Bruno Desthuilliers wrote:

You wouldn''t write something like 2.__add__(3), would you ?

Don''t give the "it''s only OO if I write obj.method(args)" crowd more bad

ideas, please ;-)

(...as Bruno implies, setattr(), len() et al can be and should be viewed

as generic functions. A specific Python implementation may use custom

code to implement behaviour for a given object; behaviour that''s more

efficient than a full Python-level method call. For example, in

CPython, len(L) is about twice as fast as L.__len__() for built-in

sequences.)

In article <48**********************@news.free.fr>,

Bruno Desthuilliers

>Mathieu Prevot a écrit :

>2008/9/4 Chris Rebert :

(snip)

>>You''re looking for the setattr() built-in function. In this exact case:

setattr(a, arg, new_value)

This is probably covered in the Python tutorial, please read it.

Regards,

Chris

Indeed.

I''ll use:

a.__setattr__(height, new_value)

Please don''t. Use the generic setattr() function instead. This holds for

any __magic__ method : they are *implementation* for operators and

generic functions - which you can think of as operators with a function

syntax -, and are not meant to be called directly. You wouldn''t write

something like 2.__add__(3), would you ?

Along with the good advice the usual suspects have given,

my intuition is that there''s an even better implementation

that doesn''t setattr() at all. While it''s impossible to

know, of course, because we don''t have the original poster''s

true requirements, I conjecture that, rather than "to link

this [user-supplied] word to a class variable", what will

serve him best is to regard the user text as an index into

a class dictionary.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值