在使用变量名之前将它们存储在列表中? [英] storing variable names in a list before they are used?

查看:129
本文介绍了在使用变量名之前将它们存储在列表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想要这样的列表:


[(first_name,''名字:''),(last_name,''姓氏:'')。 ....]


其中每个元组的第一部分是变量名称,第二部分是b / b $ b $部分是用户要看的标签,例如作为这样的形式:


名字:________

姓氏:________


(变量会存储在每个标签右侧的文本中输入的任何信息

框。我这样做,所以我可以写一个

循环来构建我的GUI组件)。


如何将这些变量名称放在列表中?我知道我

不能像上面那样离开它们,但是如果我把它们作为一个字符串放入,那么怎么做呢?b $ b我后来转换了它们分配给一个实际变量用于赋值,例如:


first_name = widget.get_text()


是否有某种成语这种工作?


谢谢。

If I want to have a list like this:

[(first_name, ''First Name:''), (last_name, ''Last Name:'').....]

where the first part of each tuple is a variable name and the second
part is a label for the user to see, such as a form like this:

First Name: ________
Last Name: ________

(the variables would store whatever information is entered in the text
boxes to the right of each label. I''m doing it this way so I can write a
loop to construct my GUI components).

how would I go about putting these variable names in a list? I know I
can''t leave them as above, but if I put them in as a string, then how do
I later "transform" them into an actual variable for assign, such as:

first_name = widget.get_text()

Is there some kind of idiom that does this sort of work?

Thanks.

推荐答案

嗨约翰


John Salerno写道:
Hi John

John Salerno wrote:

我如何将这些变量名称放在列表中?我知道我

不能像上面那样离开它们,但是如果我把它们作为一个字符串放入,那么怎么做呢?b $ b我后来转换了它们分配给一个实际变量用于赋值,例如:


first_name = widget.get_text()


是否有某种成语这种工作?
how would I go about putting these variable names in a list? I know I
can''t leave them as above, but if I put them in as a string, then how do
I later "transform" them into an actual variable for assign, such as:

first_name = widget.get_text()

Is there some kind of idiom that does this sort of work?



您希望这些变量名称显示在哪个范围内?例如,

如果你想在全球范围内定义它们那么你可以执行以下




name =''first_name''


globals()[name] = widget.get_text()


print first_name

如果你想将这些变量分配给一个对象那么你可以使用

setattr():

name =''first_name''


setattr(obj,name,widget.get_text())

print obj.first_name

希望这会有所帮助
< br $>
-Farshid

What scope do you want these variable names to show up in? For example,
if you want them to be defined in the global scope then you can do the
following:

name = ''first_name''

globals()[name] = widget.get_text()

print first_name
If you want these variables to be assigned to an object then you can use
setattr():
name = ''first_name''

setattr(obj,name,widget.get_text())

print obj.first_name
Hope this helps

-Farshid




John Salerno写道:

John Salerno wrote:

如果我想要这样的列表:


[(first_name,''名字:''),(last_name,''姓氏:'').. ...]


其中每个元组的第一部分是变量名称,第二部分是
部分是用户要看的标签,例如像这样的表格:


名字:________

姓氏:________

(变量会存储在每个标签右侧的文本

框中输入的任何信息。我是这样做的,所以我可以编写一个

循环来构建我的GUI组件。


我如何设置这些变量名称在列表中?我知道我

不能像上面那样离开它们,但是如果我把它们作为一个字符串放入,那么怎么做呢?b $ b我后来转换了它们分配给一个实际变量用于赋值,例如:


first_name = widget.get_text()


是否有某种成语这种工作?


谢谢。
If I want to have a list like this:

[(first_name, ''First Name:''), (last_name, ''Last Name:'').....]

where the first part of each tuple is a variable name and the second
part is a label for the user to see, such as a form like this:

First Name: ________
Last Name: ________

(the variables would store whatever information is entered in the text
boxes to the right of each label. I''m doing it this way so I can write a
loop to construct my GUI components).

how would I go about putting these variable names in a list? I know I
can''t leave them as above, but if I put them in as a string, then how do
I later "transform" them into an actual variable for assign, such as:

first_name = widget.get_text()

Is there some kind of idiom that does this sort of work?

Thanks.



不确定但是这篇老帖子可能有帮助:
http://mail.python.org/pipermail/tut...ry/035232.html

JM

Not sure but mmaybe this older post might help:
http://mail.python.org/pipermail/tut...ry/035232.html

JM




John Salerno写道:

John Salerno wrote:

如果我想要这样的列表:


[(first_name,''名字:''),(last_name,''姓氏:'')。 ....]


其中每个元组的第一部分是变量名,第二部分是
If I want to have a list like this:

[(first_name, ''First Name:''), (last_name, ''Last Name:'').....]

where the first part of each tuple is a variable name and the second



... 。

....


不能像上面那样离开它们,但是如果我把它们作为一个字符串放入,那么怎么办呢?b
我以后 ;变换"将它们转换为赋值的实际变量,例如:


first_name = widget.get_text()
can''t leave them as above, but if I put them in as a string, then how do
I later "transform" them into an actual variable for assign, such as:

first_name = widget.get_text()



一个更好的方法这样做可能是因此使用字典:


name_map = {" First Name":None,Last Name:None}


然后分配值:


name_map [" First Name"] = widget.get_text()


或者如果是你是坚定的,你想要你原来的格式:


firstname = [无]

lastname = [无]


[(名字,''名字:''),(姓氏,''姓氏:'')]


firstname [0] = widget.get_text()


但这有点像黑客。


你在这里遇到的问题是一个概念性问题:当你把一个

列表或元组中的变量名称,它不是*存储的名称*,而是实际值的

。我想考虑使用上面的字典版本

,或者如果事情变得越来越复杂,那么创建一个类

来生成包含你想要的结构的对象:


类FormField(对象):

def __init __(self,name,text = None):

self.name = name

self.text = text


firstname = FormField(" First Name"," Default Text")

lastname = FormField(姓氏)


fields = [firstname,lastname]


lastname.text = widget.get_text()


当然可以使用对或词典来完成

(例如firstname = {" name":" First Name"," text": 默认文本};

lastname = {" name":" Last Name"}),但我认为基于类的

版本自我文件本身好一点。

A better way of doing it may be to use a dictionary thus:

name_map = {"First Name": None, "Last Name": None}

and then assign the value:

name_map["First Name"] = widget.get_text()

Or alternatively if you were adamant you wanted your original format:

firstname = [None]
lastname = [None]

[(firstname, ''First Name:''), (lastname, ''Last Name:'')]

firstname[0] = widget.get_text()

But that''s a bit of a hack.

The problem you are having here is a conceptual one: When you put a
variable name in a list or tuple, it isn''t the *name* that''s stored but
the actual value. I would think about using the dictionary version
above, or if things are getting more complicated, then create a class
to produce objects that contain the structure you want:

class FormField(object):
def __init__(self, name, text=None):
self.name = name
self.text = text

firstname = FormField("First Name", "Default Text")
lastname = FormField("Last Name")

fields = [firstname, lastname]

lastname.text = widget.get_text()

The same of course could be accompished using pairs or dictionaries
(e.g. firstname = {"name": "First Name", "text": "Default Text"};
lastname = {"name": "Last Name"} ), but I think that the class based
version self documents itself a bit better.


这篇关于在使用变量名之前将它们存储在列表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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