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