Re:使用str作为变量名 [英] Re: use str as variable name

查看:167
本文介绍了Re:使用str作为变量名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

2008/9/4 Chris Rebert< cv ****** @ gmail.com>:


On Thu,Sep 4,2008 at 12 :25 AM,Mathieu Prevot

< ma ************ @ gmail.comwrote:


>

我有一个程序以一个单词作为参数,我想将这个单词链接到一个类变量。

例如。
类foo():



你应该子类化''object'',所以应该是:

class Foo(对象):


> width = 10
height = 20
a = foo()
arg =''height''
a .__ argname __ = new_value


您正在寻找setattr()内置函数。在这个确切的情况下:

setattr(a,arg,new_value)


这可能包含在Python教程中,请阅读它。


问候,

Chris



确实。


我''lll使用:

a .__ setattr __(height,new_value)


感谢所有

Mathieu

解决方案

Mathieu Prevotaécrit:


2008/9/4 Chris Rebert< cv *** ***@gmail.com>:



(剪辑)


>您正在寻找setattr()内置函数。在这个确切的情况下:
setattr(a,arg,new_value)

这可能包含在Python教程中,请阅读它。

问候,
Chris



确实。


我会用:

a .__ setattr__ (身高,新价值)



请不要。请改用通用的setattr()函数。这适用于

任何__magic__方法:它们是*实现*用于运算符和

泛型函数 - 您可以将其视为具有函数的运算符

语法 - ,并不意味着直接调用。你不会写

类似2 .__添加__(3),是吗?


Bruno Desthuilliers写道:


你不会写2 .__添加__(3)之类的东西,对吗?



如果我写obj.method(args),就不要给它只有OO人群更糟糕

想法,请;-)


(......如布鲁诺暗示的那样,setattr(),len()等可以而且应该被视为
作为泛型函数。特定的Python实现可以使用自定义的

代码来实现给定对象的行为;更多的行为

比完整的Python级方法调用高效。例如,在
CPython中,len(L)的速度大约是内置的L .__ len __()的两倍

序列。)


< / F>


文章< 48 *** *******************@news.free.fr> ;,

Bruno Desthuilliers< br ********** **********@websiteburo.invalidwrote:


> Mathieu Prevotaécrit:


> 2008/9/4 Chris Rebert< cv ****** @ gmail.com>:


(剪辑)


>>您正在寻找setattr()内置函数。在这个确切的情况下:
setattr(a,arg,new_value)

这可能包含在Python教程中,请阅读它。

问候,
Chris


确实。

我会用:
a .__ setattr __(height,new_value)


请不要。请改用通用的setattr()函数。这适用于
任何__magic__方法:它们是*实现*用于运算符和
泛型函数 - 您可以将其视为具有函数
语法的运算符 - 并且不打算被调用直。你不会写像2 .__添加__(3),你会吗?



除了通常的嫌疑人给出的好建议外,

我的直觉是,有一个更好的实现

根本没有setattr()。虽然我们不可能知道
,当然,因为我们没有原始海报'

真正的要求,我猜想,而不是将这个[用户提供的]单词链接到一个类变量,将b / b
最好地服务于他的是将用户文本视为
a class dictionary。


2008/9/4 Chris Rebert <cv******@gmail.com>:

On Thu, Sep 4, 2008 at 12:25 AM, Mathieu Prevot
<ma************@gmail.comwrote:

>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 <cv******@gmail.com>:

(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.)

</F>


In article <48**********************@news.free.fr>,
Bruno Desthuilliers <br********************@websiteburo.invalidwrote :

>Mathieu Prevot a écrit :

>2008/9/4 Chris Rebert <cv******@gmail.com>:


(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.


这篇关于Re:使用str作为变量名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆