Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我可以通过连接变量名来使用预定义变量吗?_Python_Variables - Fatal编程技术网

Python 我可以通过连接变量名来使用预定义变量吗?

Python 我可以通过连接变量名来使用预定义变量吗?,python,variables,Python,Variables,我有这样的变量对 KEY_PasswordExpiryFeature = "PasswordExpiryFeature" KEY_PasswordExpiryFeature_DEFAULT = "FALSE" 我调用了一些方法,其中我必须指定这两个变量。 然而,为了避免一些无脑的输入,我只想指定第一个变量 KEY_PasswordExpiryFeature = "PasswordExpiryFeature" 在接收方法中,您可以这样做 def evaluate(et, key): p

我有这样的变量对

KEY_PasswordExpiryFeature = "PasswordExpiryFeature"
KEY_PasswordExpiryFeature_DEFAULT = "FALSE"
我调用了一些方法,其中我必须指定这两个变量。 然而,为了避免一些无脑的输入,我只想指定第一个变量

KEY_PasswordExpiryFeature = "PasswordExpiryFeature"
在接收方法中,您可以这样做

def evaluate(et, key):
    print("Evaluating key: %s -> Passed: %s" % (getKeyAttribute(et, key), getValueAttribute(et, key).lower() != VALUEOFVARIABLE("KEY_" + key + "_DEFAULT")))
而不是这个

def evaluate(et, key, default_value):
    print("Evaluating key: %s -> Passed: %s" % (getKeyAttribute(et, key), getValueAttribute(et, key).lower() != default_value))

而不是传递原始变量。您可能希望将变量对封装到其他对象中。两个常见的选项是字典或命名元组。例如:

KEY_PasswordExpiry = {
    "feature": "PasswordExpiryFeature",
    "default": False
}

在namedtuple情况下,您可以使用点符号访问PasswordExpiry键的功能和默认值

>>> PasswordExpiry.feature
'PasswordExpiryFeature'
>>> PasswordExpiry.default
False
现在还不完全清楚evaluate应该做什么。et是什么东西?密钥应该是类似于密钥\u PasswordExpiryFeature的东西吗?
>>> PasswordExpiry.feature
'PasswordExpiryFeature'
>>> PasswordExpiry.default
False