For example, in the code below I would like to obtain the list [1,2,3] using x as a reference.
例如,在下面的代码中,我希望使用x作为引用获取列表[1,2,3]。
In[1]: pasta=[1,2,3]
In:[2]: pasta
Out[2]: [1, 2, 3]
In [3]: x='pas'+'ta'
In [4]: x
Out[4]: 'pasta'
4 个解决方案
#1
13
What you are trying to do is a bad practice.
你想做的是一个坏习惯。
What you really need is a dict
:
你真正需要的是一个法令:
>>> dct = {'pasta': [1,2,3]}
>>> x = 'pas' + 'ta'
>>> dct[x]
[1, 2, 3]
This is the right data structure for the actual task you're trying to achieve: using a string to access an object.
这是您试图实现的实际任务的正确数据结构:使用字符串访问对象。
Other answers suggested (or just showed with a worning) different ways to do that. Since Python is a very flexible language, you can almost always found such different ways to follow for a given task, but "there should be one-- and preferably only one --obvious way to do it"[1].
其他的回答则建议(或者只是用一种令人难堪的方式)采用不同的方式。由于Python是一种非常灵活的语言,对于给定的任务,您几乎总是可以找到如此不同的方法,但是“应该有一种——最好只有一种——显然是这么做的”[1]。
All of them will do the work, but not without downsides:
他们都将做这项工作,但并非没有缺点:
-
locals()
is less readable, needlessly complex and also open to risks in some cases (see Mark Byers answer). If you uselocals()
you are going to mix the real variables with the database ones, it's messy. - 局部变量()可读性较差,不必要地复杂,在某些情况下也容易出现风险(参见Mark Byers的回答)。如果您使用local(),您将会将实际的变量与数据库的变量混合在一起,这很麻烦。
-
eval()
is plain ugly, is a "quick-and-dirty way to get some source code dynamically"[2] and a bad practice. - eval()非常难看,它是“动态获取源代码的快速而肮脏的方法”[2],这是一个糟糕的实践。
When in doubt about the right way to choose, tring to follow the Zen of Python might be a start.
当对正确的选择方式有疑问时,尝试遵循Python的禅宗或许是一个开始。
And hey, even the InteractiveInterpreter
could be used to access an object using a string, but that doesn't mean I'm going to.
即使是交互式解释器也可以用字符串访问对象,但这并不意味着我要。
#2
2
Like other pointed out, you should normally avoid doing this and just use either a dictionary (in an example like you give) or in some cases a list (for example instead of using my_var1, my_var2, my_var3
-> my_vars
).
与其他指出的一样,您通常应该避免这样做,只使用字典(在您给出的示例中)或列表(例如,不使用my_var1、my_var2、my_var3 -> my_vars)。
However if you still want to do that you have a couple of option.
然而,如果你还想这么做,你有两个选择。
Your could do:
你能做的:
locals()[x]
or
或
eval(x) #always make sure you do proper validation before using eval. A very powerfull feature of python imo but very risky if used without care.
If the pasta is an object attribute you can get it safely by:
如果意大利面是一个对象属性,您可以通过以下方法安全地获得:
getattr(your_obj, x)
#3
2
Well, to do what you literally asked for, you could use locals
:
要做到字面上的要求,你可以用当地人:
>>> locals()[x]
[1, 2, 3]
However it is almost always a bad idea to do this. As Sven Marnach pointed out in the comments: Keep data out of your variable names. Using variables as data could also be a security risk. For example, if the name of the variable comes from the user they might be able to read or modify variables that you never intended them to have access to. They just need to guess the variable name.
然而,这样做几乎总是一个坏主意。正如Sven Marnach在评论中指出的:将数据从变量名中删除。使用变量作为数据也是一种安全风险。例如,如果变量的名称来自用户,那么他们可能能够读取或修改您从未打算让他们访问的变量。他们只需要猜出变量名。
It would be much better to use a dictionary instead.
用字典代替会好得多。
>>> your_dict = {}
>>> your_dict['pasta'] = [1, 2, 3]
>>> x = 'pas' + 'ta'
>>> your_dict[x]
[1, 2, 3]
#4
0
Use this
使用这个
hello = [1,2,3]
print vars()['hello']
Returns [1, 2, 3]
.
返回(1、2、3)。
#1
13
What you are trying to do is a bad practice.
你想做的是一个坏习惯。
What you really need is a dict
:
你真正需要的是一个法令:
>>> dct = {'pasta': [1,2,3]}
>>> x = 'pas' + 'ta'
>>> dct[x]
[1, 2, 3]
This is the right data structure for the actual task you're trying to achieve: using a string to access an object.
这是您试图实现的实际任务的正确数据结构:使用字符串访问对象。
Other answers suggested (or just showed with a worning) different ways to do that. Since Python is a very flexible language, you can almost always found such different ways to follow for a given task, but "there should be one-- and preferably only one --obvious way to do it"[1].
其他的回答则建议(或者只是用一种令人难堪的方式)采用不同的方式。由于Python是一种非常灵活的语言,对于给定的任务,您几乎总是可以找到如此不同的方法,但是“应该有一种——最好只有一种——显然是这么做的”[1]。
All of them will do the work, but not without downsides:
他们都将做这项工作,但并非没有缺点:
-
locals()
is less readable, needlessly complex and also open to risks in some cases (see Mark Byers answer). If you uselocals()
you are going to mix the real variables with the database ones, it's messy. - 局部变量()可读性较差,不必要地复杂,在某些情况下也容易出现风险(参见Mark Byers的回答)。如果您使用local(),您将会将实际的变量与数据库的变量混合在一起,这很麻烦。
-
eval()
is plain ugly, is a "quick-and-dirty way to get some source code dynamically"[2] and a bad practice. - eval()非常难看,它是“动态获取源代码的快速而肮脏的方法”[2],这是一个糟糕的实践。
When in doubt about the right way to choose, tring to follow the Zen of Python might be a start.
当对正确的选择方式有疑问时,尝试遵循Python的禅宗或许是一个开始。
And hey, even the InteractiveInterpreter
could be used to access an object using a string, but that doesn't mean I'm going to.
即使是交互式解释器也可以用字符串访问对象,但这并不意味着我要。
#2
2
Like other pointed out, you should normally avoid doing this and just use either a dictionary (in an example like you give) or in some cases a list (for example instead of using my_var1, my_var2, my_var3
-> my_vars
).
与其他指出的一样,您通常应该避免这样做,只使用字典(在您给出的示例中)或列表(例如,不使用my_var1、my_var2、my_var3 -> my_vars)。
However if you still want to do that you have a couple of option.
然而,如果你还想这么做,你有两个选择。
Your could do:
你能做的:
locals()[x]
or
或
eval(x) #always make sure you do proper validation before using eval. A very powerfull feature of python imo but very risky if used without care.
If the pasta is an object attribute you can get it safely by:
如果意大利面是一个对象属性,您可以通过以下方法安全地获得:
getattr(your_obj, x)
#3
2
Well, to do what you literally asked for, you could use locals
:
要做到字面上的要求,你可以用当地人:
>>> locals()[x]
[1, 2, 3]
However it is almost always a bad idea to do this. As Sven Marnach pointed out in the comments: Keep data out of your variable names. Using variables as data could also be a security risk. For example, if the name of the variable comes from the user they might be able to read or modify variables that you never intended them to have access to. They just need to guess the variable name.
然而,这样做几乎总是一个坏主意。正如Sven Marnach在评论中指出的:将数据从变量名中删除。使用变量作为数据也是一种安全风险。例如,如果变量的名称来自用户,那么他们可能能够读取或修改您从未打算让他们访问的变量。他们只需要猜出变量名。
It would be much better to use a dictionary instead.
用字典代替会好得多。
>>> your_dict = {}
>>> your_dict['pasta'] = [1, 2, 3]
>>> x = 'pas' + 'ta'
>>> your_dict[x]
[1, 2, 3]
#4
0
Use this
使用这个
hello = [1,2,3]
print vars()['hello']
Returns [1, 2, 3]
.
返回(1、2、3)。