更改局部变量的值,其中变量名将表示为字符串 [英] Change the value of a local variable where variable name will be expressed as a string

查看:38
本文介绍了更改局部变量的值,其中变量名将表示为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到一种使用字符串替换来调用变量以设置其值的良好且安全的方法.这是一种更佳的做法,我正在寻找最好的方法来做到这一点.

I am trying to find a good and secure way to call a variable using string substitution in order to set its value. This is a more best-practices and I am looking for the best possible way to do this.

首先,使用字典或类似的结构不是我想要的.我想设置一个变量的值!

First of all, using dictionary or similar structure is not what I want. I wish to set the value of a variable!

示例;

def test_my_var(name_param, val):
    # Set initial values for each variable
    my_var_1 = -1
    my_var_2 = -1
    my_var_3 = -1
    ...
    my_var_n = -1
   # Now I will update the value of mth variable 
   my_var_%{name_param}s = val  # Of course this will not work!!
   # Now print the new value of this variable
   print my_var_%{name_param}s

我正在寻找一种实用的方法来实现这一目标.经过测试但不起作用的事情是:

I am looking for a practical way to accomplish this. Things that are tested and do not work are:

我不想将我的变量保留在 dict 或类似结构中.它们是函数内部的局部变量,应该保持原样.

I do not want to keep my vars in dict or similar structure. They are local variables inside a function and they should stay as they are.

我不喜欢使用 exec ,因为它会执行发送的任何命令!所以下面不是一个好的方法,也不是一个有效的方法.

Using exec is not something I prefer since it will execute any command that is sent! So below is not a good way to go and is not a valid way.

def test_case(index, val):
    var_1 = -1
    var_2 = -1
    exec("var_%s" = %s % (index, val))
    print eval("var_%s" % index)  

测试了使用 locals() 和设置值的方法,但没有这样做

Tested ways of using locals() and in setting the value, but fail to do so

def test_case(index, val):
    val_1 = -1
    val_2 = -1
    print locals()["val_%s" % index]
    locals()["val_%s" % index] = val
    print val_1, val_2  # That will not update the calue of the variable `val_1` or any other variable

所以我正在寻找一种聪明的方法来更改变量的值,其中变量名将表示为字符串替换.

So I am looking for a smart way to change the value of a variable where variable name will be expressed as a string substitution.

推荐答案

你不能做你想做的,不在你设置的约束范围内.

You cannot do what you want, not within the constraints you set.

那是因为 locals() 不是活动对象.CPython 使用 C 数组作为局部变量,字节码使用索引来访问它们.locals() 字典是一种方便的调试工具,它将存储的本地名称列表与数组引用的值配对.尝试改变该字典不会更新实际的局部变量数组.

That's because locals() is not a live object. CPython uses a C array for the local variables, with the bytecode using indices to access them. The locals() dictionary is a convenience, a debugging tool, which pairs up the stored list of names for the locals with the values the array references. Trying to mutate that dictionary does not update the actual local variable array.

正如您所发现的,exec 确实有效,因为在这种情况下,Python 2 禁用了对局部变量的优化,并在您将 exec 添加到组合中时使用较慢的技术.在 Python 3 中,exec() 不再是一个语句而是一个函数,这个漏洞被堵住了.

As you figured out, exec does work, because Python 2 disables the optimisation for locals in that case and uses slower techniques the moment you add exec into the mix. In Python 3, exec() is no longer a statement but a function instead, and this loophole was closed.

请参阅globals() 与 locals() 可变性Python 2 和 Python 3 中 exec 函数的行为.

因此,如果您真的在寻找最佳实践,请坚持使用字典或列表.这些都是局部变量也是,但是你实际上可以通过字符串键或整数索引来寻址其中的值.

So if you are really looking for a best practice, stick with a dictionary or a list. These are locals too, but ones you can actually address the values in by string key or integer index.

这篇关于更改局部变量的值,其中变量名将表示为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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