【问题标题】:str value to reference of variable name and assignment变量名和赋值引用的 str 值
【发布时间】:2021-08-28 10:23:34
【问题描述】:

我正在制作一个 CLI 程序。所以我需要用户看到他们的参数已经改变了。然后我存储一个元数据文件,显示他们的参数传递与实际运行的参数,以及其他信息。

我希望能够执行无牵引异常处理,在这种情况下,我的程序可以确保一组确定性的变量值满足它们的最低要求。

到目前为止,我有这个:

def my_function(datasets, samples, columns, n_cks, cks_length, non_unique):
    exception_handling = [['datasets', 1], ['samples', 2], ['columns', 3], ['n_cks', 1], ['cks_length', 2], [non_unique, 0.01]]

    for param, minim_req in exception_handling:
        if not exec(param) >= minim_req:
            exec(param) = minim_req
            print("'" + str(param) + "' was too small...")
            print("'" + str(param) + "' = " + minim_req)

错误:

SyntaxError: cannot assign to function call

在两个地方都尝试eval() 而不是exec() 时出现同样的错误。


读过使用locals(),我也在这里偶然发现。

解决方法 2 - 使用 locals():

def my_function(datasets, samples, columns, n_cks, cks_length, non_unique):
    #exception_handling = [['datasets', 1], ['samples', 2], ['columns', 3], ['n_cks', 1], ['cks_length', 2]]
    minim_req = {'datasets': 1, 'samples': 2, 'columns': 3, 'n_cks': 1, 'cks_length': 2, 'non_unique': 0.01}
    
    params = locals()
    print(params)
    
    for key, value in params:
        if not value >= minim_req[key]:
            params[key] = minim_req[key]
            print("'" + str(key) + "' was too small...")
            print("'" + str(key) + "' = " + minim_req[key])

错误:

ValueError: too many values to unpack (expected 2)

【问题讨论】:

  • 请解释大局,我认为您以错误的方式处理问题。
  • @BeChillerToo 我想确保传递的这些参数至少是某个值。我已经有异常处理来确保它们的数据类型和存在。
  • exec 用于方程。对于前-r = 3a = "r = 0"exec(a)
  • 它是一个 CLI 程序。所以我需要用户看到他们的参数已经改变了。然后我存储一个元数据文件,显示他们的参数传递与实际运行的参数,以及其他信息。
  • 请发送您的完整代码。什么是数据集等

标签: python variables exec eval assign


【解决方案1】:

如果你所有的变量都是 int,试试这样的:

for param, minim_req in exception_handling:
    length = exec(param)
    if length < minim_req:
        # Do something with 'length'

【讨论】:

  • 我得到 TypeError: 'int' 类型的对象没有 len()
  • 更新了我的答案
  • 其中一个是浮点数:(
  • 是的,我认为没关系
  • my_function(0, 0, 0, 0, 0, 0.0) 得到TypeError: '&lt;' not supported between instances of 'NoneType' and 'int'
【解决方案2】:
datasets, samples, columns, n_cks, cks_length, non_unique = 0, 0, 0, 0, 0, 0.0

params = [['datasets', datasets], ['samples', samples], ['columns', columns], ['n_cks', n_cks], ['cks_length', cks_length], ['non_unique', non_unique]]
minim_req = [1, 2, 3, 1, 2, 0.01]

for (i, val), m in zip(enumerate(params), minim_req):
    if not val[1] >= m:
        params[i][1] = m
        print("'" + params[i][0] + "' was too small...")
        print("'" + params[i][0] + "' = " + str(m) + "\n")
        
datasets, samples, columns, n_cks, cks_length, non_unique = params[0], params[1], params[2], params[3], params[4], params[5]
print(datasets, samples, columns, n_cks, cks_length, non_unique)
>>> 1 2 3 1 2 0.01

这里唯一的问题是这依赖于一组静态/固定的变量名;对我来说足够了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-10
    • 2016-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多